kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
895 stars 237 forks source link

Allow openapi-types extensions of other things than `OperationObject` #836

Open jonaskello opened 2 years ago

jonaskello commented 2 years ago

It seems today extensions such as x-myexension is only possible to pass through to OperationObject<T> by putting it in T of Document<T>. However openapi v3 supports extensions in the following places according to the docs:

Specifically I'm trying to extend securitySchemes which does not seem possible today?

jonaskello commented 2 years ago

As a temporary work-around I made this mapped type which allows for extension of SecuritySchemeObject:

import { OpenAPIV3 } from "openapi-types";

export type DocumentEx<TOperationEx extends {} = {}, TSecuritySchemeEx extends {} = {}> = {
  [P in keyof OpenAPIV3.Document<TOperationEx>]: P extends "components"
    ? MapComponents<OpenAPIV3.Document<TOperationEx>[P], TSecuritySchemeEx>
    : OpenAPIV3.Document<TOperationEx>[P];
};

type MapComponents<T, TSecuritySchemeEx extends {}> = {
  [P in keyof T]: P extends "securitySchemes" ? MapSecuritySchemes<T[P], TSecuritySchemeEx> : T[P];
};

type MapSecuritySchemes<T, TSecuritySchemeEx extends {}> = {
  [P in keyof T]: MapSecuritySchemeObject<T[P], TSecuritySchemeEx>;
};

type MapSecuritySchemeObject<T, TSecuritySchemeEx extends {}> = T extends OpenAPIV3.SecuritySchemeObject
  ? T & TSecuritySchemeEx
  : T;