Skip to content

feat: introduce excludeDeprecated option #1111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/openapi-typescript/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options
--support-array-length (optional) Generate tuples using array minItems / maxItems
--path-params-as-types (optional) Substitute path parameter names with their respective types
--alphabetize (optional) Sort types alphabetically
--exclude-deprecated (optional) Exclude deprecated fields from types
`;

const OUTPUT_FILE = "FILE";
Expand Down Expand Up @@ -54,6 +55,7 @@ const flags = parser(args, {
"supportArrayLength",
"pathParamsAsTypes",
"alphabetize",
"excludeDeprecated",
],
string: ["auth", "header", "headersObject", "httpMethod"],
alias: {
Expand Down Expand Up @@ -104,6 +106,7 @@ async function generateSchema(pathToSpec) {
supportArrayLength: flags.supportArrayLength,
pathParamsAsTypes: flags.pathParamsAsTypes,
alphabetize: flags.alphabetize,
excludeDeprecated: flags.excludeDeprecated,
});

// output
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op
parameters: {},
silent: options.silent ?? false,
supportArrayLength: options.supportArrayLength ?? false,
excludeDeprecated: options.excludeDeprecated ?? false,
};

// note: we may be loading many large schemas into memory at once; take care to reuse references without cloning
Expand Down Expand Up @@ -129,7 +130,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op
if (!Object.keys(subschemaTypes).length) break;
output.push(indent(`${key}: {`, indentLv));
indentLv++;
for (const [k, v] of getEntries(subschemaTypes, options.alphabetize)) {
for (const [k, v] of getEntries(subschemaTypes, options.alphabetize, options.excludeDeprecated)) {
if (EMPTY_OBJECT_RE.test(v)) output.push(indent(`${escObjKey(k)}: Record<string, never>;`, indentLv));
else output.push(indent(`${escObjKey(k)}: ${v};`, indentLv));
}
Expand Down
12 changes: 6 additions & 6 deletions packages/openapi-typescript/src/transform/components-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function transformComponentsObject(components: ComponentsObject,
if (components.schemas) {
output.push(indent("schemas: {", indentLv));
indentLv++;
for (const [name, schemaObject] of getEntries(components.schemas, ctx.alphabetize)) {
for (const [name, schemaObject] of getEntries(components.schemas, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(schemaObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escObjKey(name);
Expand All @@ -37,7 +37,7 @@ export default function transformComponentsObject(components: ComponentsObject,
if (components.responses) {
output.push(indent("responses: {", indentLv));
indentLv++;
for (const [name, responseObject] of getEntries(components.responses, ctx.alphabetize)) {
for (const [name, responseObject] of getEntries(components.responses, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(responseObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escObjKey(name);
Expand All @@ -62,7 +62,7 @@ export default function transformComponentsObject(components: ComponentsObject,
if (components.parameters) {
output.push(indent("parameters: {", indentLv));
indentLv++;
for (const [name, parameterObject] of getEntries(components.parameters, ctx.alphabetize)) {
for (const [name, parameterObject] of getEntries(components.parameters, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(parameterObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escObjKey(name);
Expand Down Expand Up @@ -90,7 +90,7 @@ export default function transformComponentsObject(components: ComponentsObject,
if (components.requestBodies) {
output.push(indent("requestBodies: {", indentLv));
indentLv++;
for (const [name, requestBodyObject] of getEntries(components.requestBodies, ctx.alphabetize)) {
for (const [name, requestBodyObject] of getEntries(components.requestBodies, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(requestBodyObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escObjKey(name);
Expand Down Expand Up @@ -125,7 +125,7 @@ export default function transformComponentsObject(components: ComponentsObject,
if (components.headers) {
output.push(indent("headers: {", indentLv));
indentLv++;
for (const [name, headerObject] of getEntries(components.headers, ctx.alphabetize)) {
for (const [name, headerObject] of getEntries(components.headers, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(headerObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escObjKey(name);
Expand All @@ -150,7 +150,7 @@ export default function transformComponentsObject(components: ComponentsObject,
if (components.pathItems) {
output.push(indent("pathItems: {", indentLv));
indentLv++;
for (const [name, pathItemObject] of getEntries(components.pathItems, ctx.alphabetize)) {
for (const [name, pathItemObject] of getEntries(components.pathItems, ctx.alphabetize, ctx.excludeDeprecated)) {
let key = escObjKey(name);
if (ctx.immutableTypes) key = tsReadonly(key);
if ("$ref" in pathItemObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function transformHeaderObject(headerObject: HeaderObject, { path
let { indentLv } = ctx;
const output: string[] = ["{"];
indentLv++;
for (const [contentType, mediaTypeObject] of getEntries(headerObject.content, ctx.alphabetize)) {
for (const [contentType, mediaTypeObject] of getEntries(headerObject.content, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(mediaTypeObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escStr(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default function transformOperationObject(operationObject: OperationObjec
if (operationObject.responses) {
output.push(indent(`responses: {`, indentLv));
indentLv++;
for (const [responseCode, responseObject] of getEntries(operationObject.responses, ctx.alphabetize)) {
for (const [responseCode, responseObject] of getEntries(operationObject.responses, ctx.alphabetize, ctx.excludeDeprecated)) {
const key = escObjKey(responseCode);
const c = getSchemaObjectComment(responseObject, indentLv);
if (c) output.push(indent(c, indentLv));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function transformPathsObject(pathsObject: PathsObject, ctx: Glob
let { indentLv } = ctx;
const output: string[] = ["{"];
indentLv++;
for (const [url, pathItemObject] of getEntries(pathsObject, ctx.alphabetize)) {
for (const [url, pathItemObject] of getEntries(pathsObject, ctx.alphabetize, ctx.excludeDeprecated)) {
let path = url;

// build dynamic string template literal index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function transformRequestBodyObject(requestBodyObject: RequestBod
output.push(indent(ctx.immutableTypes ? tsReadonly("content: {") : "content: {", indentLv));
if (!Object.keys(requestBodyObject.content).length) return `${escStr("*/*")}: never`;
indentLv++;
for (const [contentType, mediaTypeObject] of getEntries(requestBodyObject.content, ctx.alphabetize)) {
for (const [contentType, mediaTypeObject] of getEntries(requestBodyObject.content, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(mediaTypeObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escStr(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function transformResponseObject(responseObject: ResponseObject,
indentLv++;
output.push(indent(`headers: {`, indentLv));
indentLv++;
for (const [name, headerObject] of getEntries(responseObject.headers, ctx.alphabetize)) {
for (const [name, headerObject] of getEntries(responseObject.headers, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(headerObject, indentLv);
if (c) output.push(indent(c, indentLv));
let key = escObjKey(name);
Expand Down Expand Up @@ -52,7 +52,7 @@ export default function transformResponseObject(responseObject: ResponseObject,
indentLv++;
output.push(indent("content: {", indentLv));
indentLv++;
for (const [contentType, mediaTypeObject] of getEntries(responseObject.content, ctx.alphabetize)) {
for (const [contentType, mediaTypeObject] of getEntries(responseObject.content, ctx.alphabetize, ctx.excludeDeprecated)) {
let key = escStr(contentType);
if (ctx.immutableTypes) key = tsReadonly(key);
output.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function defaultSchemaObjectTransform(schemaObject: SchemaObject | Refere
const coreType: string[] = [];
if (("properties" in schemaObject && schemaObject.properties && Object.keys(schemaObject.properties).length) || ("additionalProperties" in schemaObject && schemaObject.additionalProperties)) {
indentLv++;
for (const [k, v] of getEntries(schemaObject.properties ?? {}, ctx.alphabetize)) {
for (const [k, v] of getEntries(schemaObject.properties ?? {}, ctx.alphabetize, ctx.excludeDeprecated)) {
const c = getSchemaObjectComment(v, indentLv);
if (c) coreType.push(indent(c, indentLv));
let key = escObjKey(k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function transformWebhooksObject(webhooksObject: WebhooksObject,
let { indentLv } = ctx;
const output: string[] = ["{"];
indentLv++;
for (const [name, pathItemObject] of getEntries(webhooksObject, ctx.alphabetize)) {
for (const [name, pathItemObject] of getEntries(webhooksObject, ctx.alphabetize, ctx.excludeDeprecated)) {
output.push(
indent(
`${escStr(name)}: ${transformPathItemObject(pathItemObject, {
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-typescript/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ export interface OpenAPITSOptions {
* function if available; else, it will use unidici's fetch function.
*/
fetch?: Fetch;
/** Exclude deprecated fields from types? (default: false) */
excludeDeprecated?: boolean;
}

/** Subschema discriminator (note: only valid $ref types accepted here) */
Expand Down Expand Up @@ -661,6 +663,7 @@ export interface GlobalContext {
pathParamsAsTypes: boolean;
silent: boolean;
supportArrayLength: boolean;
excludeDeprecated: boolean;
}

// Fetch is available in the global scope starting with Node v18.
Expand Down
5 changes: 3 additions & 2 deletions packages/openapi-typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ export function indent(input: string, level: number) {
}

/** call Object.entries() and optionally sort */
export function getEntries<T>(obj: ArrayLike<T> | Record<string, T>, alphabetize?: boolean) {
const entries = Object.entries(obj);
export function getEntries<T>(obj: ArrayLike<T> | Record<string, T>, alphabetize?: boolean, excludeDeprecated?: boolean) {
let entries = Object.entries(obj);
if (alphabetize) entries.sort(([a], [b]) => a.localeCompare(b, "en", { numeric: true }));
if (excludeDeprecated) entries = entries.filter(([, v]) => !(v && typeof v === "object" && "deprecated" in v && v.deprecated));
return entries;
}

Expand Down
31 changes: 30 additions & 1 deletion packages/openapi-typescript/test/components-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const options: GlobalContext = {
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
};

const basicSchema: ComponentsObject = {
Expand Down Expand Up @@ -235,7 +236,35 @@ describe("Components Object", () => {
}`);
});
});
});
describe("excludeDeprecated", () => {
test("true", () => {
const schema: ComponentsObject = {
schemas: {
Alpha: {
type: "object",
properties: {
a: { type: "boolean", deprecated: true },
z: { type: "boolean" },
},
},
},
};
const generated = transformComponentsObject(schema, { ...options, excludeDeprecated: true });
expect(generated).toBe(`{
schemas: {
Alpha: {
z?: boolean;
};
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
}`);
});
});
});

test("parameters with required: false", () => {
const generated = transformComponentsObject(optionalParamSchema, options);
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/test/header-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const options: TransformHeaderObjectOptions = {
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/test/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async function load(
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
...options,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const options: TransformPathItemObjectOptions = {
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/test/paths-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const options: GlobalContext = {
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
};

describe("Paths Object", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const options: TransformRequestBodyObjectOptions = {
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/test/schema-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const options: TransformSchemaObjectOptions = {
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/test/webhooks-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const options: GlobalContext = {
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
};

describe("Webhooks Object", () => {
Expand Down