Skip to content

Commit c169fa5

Browse files
committed
Schema resolver initial tags update
1 parent e973b10 commit c169fa5

8 files changed

Lines changed: 83 additions & 70 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@povio/openapi-codegen-cli",
3-
"version": "0.3.3",
3+
"version": "0.3.4",
44
"main": "./dist/index.js",
55
"bin": {
66
"openapi-codegen": "./dist/sh.js"

src/generators/const/js.const.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const RESERVED_WORDS = [
2+
"break",
3+
"case",
4+
"catch",
5+
"class",
6+
"const",
7+
"continue",
8+
"debugger",
9+
"default",
10+
"delete",
11+
"do",
12+
"else",
13+
"export",
14+
"extends",
15+
"false",
16+
"finally",
17+
"for",
18+
"function",
19+
"if",
20+
"import",
21+
"in",
22+
"instanceof",
23+
"new",
24+
"null",
25+
"return",
26+
"super",
27+
"switch",
28+
"this",
29+
"throw",
30+
"true",
31+
"try",
32+
"typeof",
33+
"var",
34+
"void",
35+
"while",
36+
"with",
37+
];

src/generators/const/openapi.const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export const ALLOWED_METHODS = [
1919
"trace",
2020
] as Array<OpenAPIV3.HttpMethods>;
2121
export const PRIMITIVE_TYPE_LIST = ["string", "number", "integer", "boolean"];
22+
export const COMPOSITE_KEYWORDS = ["allOf", "anyOf", "oneOf"] as (keyof OpenAPIV3.SchemaObject)[];

src/generators/core/SchemaResolver.class.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OpenAPIV3 } from "openapi-types";
2-
import { ALLOWED_METHODS } from "../const/openapi.const";
2+
import { ALLOWED_METHODS, COMPOSITE_KEYWORDS } from "../const/openapi.const";
33
import { GenerateOptions } from "../types/options";
44
import { pick } from "../utils/object.utils";
55
import {
@@ -155,24 +155,6 @@ export class SchemaResolver {
155155
}
156156

157157
private initializeSchemaTags() {
158-
const getSchemaRefs = (
159-
schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined,
160-
): OpenAPIV3.ReferenceObject[] => {
161-
const schemaRefObjs: OpenAPIV3.ReferenceObject[] = [];
162-
if (!schema) {
163-
return schemaRefObjs;
164-
}
165-
166-
if (isReferenceObject(schema)) {
167-
schemaRefObjs.push(schema);
168-
} else if (schema.allOf || schema.anyOf || schema.oneOf) {
169-
const schemaObjs = schema.allOf ?? schema.anyOf ?? schema.oneOf ?? [];
170-
schemaObjs.forEach((schema) => schemaRefObjs.push(...getSchemaRefs(schema)));
171-
}
172-
173-
return schemaRefObjs;
174-
};
175-
176158
for (const path in this.openApiDoc.paths) {
177159
const pathItemObj = this.openApiDoc.paths[path] as OpenAPIV3.PathItemObject;
178160

@@ -184,29 +166,30 @@ export class SchemaResolver {
184166
continue;
185167
}
186168

187-
// Collect all parameter objects that are references
188169
const schemaRefObjs = [] as OpenAPIV3.ReferenceObject[];
170+
171+
// Collect all referenced schemas in parameter objects
189172
operation.parameters?.map((param) => {
190-
schemaRefObjs.push(...getSchemaRefs((param as OpenAPIV3.ParameterObject).schema));
173+
schemaRefObjs.push(...this.getOperationSchemaRefs((param as OpenAPIV3.ParameterObject).schema));
191174
});
192175

193-
// Collect all requestBody objects that are references
176+
// Collect all referenced schemas in requestBody objects
194177
if (operation.requestBody) {
195178
const requestBodyObj = this.resolveObject(operation.requestBody);
196179
const mediaTypes = Object.keys(requestBodyObj.content ?? {});
197180
const matchingMediaType = mediaTypes.find(isParamMediaTypeAllowed);
198181
if (matchingMediaType) {
199-
schemaRefObjs.push(...getSchemaRefs(requestBodyObj.content?.[matchingMediaType]?.schema));
182+
schemaRefObjs.push(...this.getOperationSchemaRefs(requestBodyObj.content?.[matchingMediaType]?.schema));
200183
}
201184
}
202185

203-
// Collect all main response objects that are references
186+
// Collect all referenced schemas in main response objects
204187
for (const statusCode in operation.responses) {
205188
const responseObj = <OpenAPIV3.ResponseObject>this.resolveObject(operation.responses[statusCode]);
206189
const mediaTypes = Object.keys(responseObj.content ?? {});
207190
const matchingMediaType = mediaTypes.find(isMediaTypeAllowed);
208191
if (matchingMediaType) {
209-
schemaRefObjs.push(...getSchemaRefs(responseObj.content?.[matchingMediaType]?.schema));
192+
schemaRefObjs.push(...this.getOperationSchemaRefs(responseObj.content?.[matchingMediaType]?.schema));
210193
}
211194
}
212195

@@ -228,4 +211,34 @@ export class SchemaResolver {
228211
}
229212
}
230213
}
214+
215+
private getOperationSchemaRefs(
216+
schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined,
217+
): OpenAPIV3.ReferenceObject[] {
218+
if (!schema) {
219+
return [];
220+
}
221+
222+
const schemaRefObjs: OpenAPIV3.ReferenceObject[] = [];
223+
224+
if (isReferenceObject(schema)) {
225+
schemaRefObjs.push(schema);
226+
}
227+
228+
const schemaObj = schema as OpenAPIV3.SchemaObject;
229+
if (COMPOSITE_KEYWORDS.some((prop) => prop in schemaObj && schemaObj[prop])) {
230+
const schemaObjs = schemaObj.allOf ?? schemaObj.anyOf ?? schemaObj.oneOf ?? [];
231+
schemaObjs.forEach((schema) => schemaRefObjs.push(...this.getOperationSchemaRefs(schema)));
232+
}
233+
if (schemaObj.properties) {
234+
Object.values(schemaObj.properties).forEach((schema) =>
235+
schemaRefObjs.push(...this.getOperationSchemaRefs(schema)),
236+
);
237+
}
238+
if (schemaObj.type === "array") {
239+
schemaRefObjs.push(...this.getOperationSchemaRefs((schema as OpenAPIV3.ArraySchemaObject).items));
240+
}
241+
242+
return schemaRefObjs;
243+
}
231244
}

src/generators/core/openapi/getSchemaRefsDependencyGraph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { OpenAPIV3 } from "openapi-types";
2+
import { COMPOSITE_KEYWORDS } from "src/generators/const/openapi.const";
23
import { isReferenceObject } from "src/generators/utils/openapi.utils";
34

45
export function getSchemaRefsDependencyGraph({
@@ -51,15 +52,14 @@ function visit({
5152
return;
5253
}
5354

54-
const props = ["allOf", "oneOf", "anyOf"] as const;
55-
for (const prop of props) {
55+
for (const prop of COMPOSITE_KEYWORDS) {
5656
if (schema[prop]) {
5757
for (const item of schema[prop]!) {
5858
visit({ ...params, schema: item });
5959
}
6060
}
6161
}
62-
if (props.some((prop) => schema[prop])) {
62+
if (COMPOSITE_KEYWORDS.some((prop) => schema[prop])) {
6363
return;
6464
}
6565

src/generators/utils/js.utils.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,3 @@
1-
export const RESERVED_WORDS = [
2-
"break",
3-
"case",
4-
"catch",
5-
"class",
6-
"const",
7-
"continue",
8-
"debugger",
9-
"default",
10-
"delete",
11-
"do",
12-
"else",
13-
"export",
14-
"extends",
15-
"false",
16-
"finally",
17-
"for",
18-
"function",
19-
"if",
20-
"import",
21-
"in",
22-
"instanceof",
23-
"new",
24-
"null",
25-
"return",
26-
"super",
27-
"switch",
28-
"this",
29-
"throw",
30-
"true",
31-
"try",
32-
"typeof",
33-
"var",
34-
"void",
35-
"while",
36-
"with",
37-
];
38-
391
export const isValidPropertyName = (str: string) => /^(?:[a-zA-Z_$][a-zA-Z0-9_$]*|[0-9]+)$/.test(str);
402

413
export const invalidVariableNameCharactersToCamel = (str: string) =>

src/generators/utils/openapi.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { OpenAPIV3 } from "openapi-types";
22
import { match, P } from "ts-pattern";
3+
import { RESERVED_WORDS } from "../const/js.const";
34
import { ALLOWED_METHODS, ALLOWED_PARAM_MEDIA_TYPES, PRIMITIVE_TYPE_LIST } from "../const/openapi.const";
45
import { PrimitiveType, SingleType } from "../types/openapi";
56
import { GenerateOptions } from "../types/options";
6-
import { RESERVED_WORDS } from "./js.utils";
77
import { pick } from "./object.utils";
88
import { capitalize, kebabToCamel, nonWordCharactersToCamel, snakeToCamel } from "./string.utils";
99

src/generators/utils/ts.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { OpenAPIV3 } from "openapi-types";
22
import { match } from "ts-pattern";
3+
import { COMPOSITE_KEYWORDS } from "../const/openapi.const";
34
import { SchemaResolver } from "../core/SchemaResolver.class";
45
import { GenerateType } from "../types/generate";
56
import { TsMetaType, TsObjectMetaType, TsProperty, TsType, TsTypeBase } from "../types/metadata";
@@ -64,8 +65,7 @@ export function getSchemaTsMetaType({
6465
return { metaType: "primitive" };
6566
}
6667

67-
const compositeKeywords = ["allOf", "anyOf", "oneOf"] as (keyof OpenAPIV3.SchemaObject)[];
68-
for (const compositeKeyword of compositeKeywords) {
68+
for (const compositeKeyword of COMPOSITE_KEYWORDS) {
6969
const compositeObjs = schema[compositeKeyword] as (OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject)[];
7070
if (!compositeObjs) {
7171
continue;

0 commit comments

Comments
 (0)