Skip to content

Commit e973b10

Browse files
committed
Fix schema resolver initial tags to contain reference schemas from composites
1 parent f983c2c commit e973b10

2 files changed

Lines changed: 23 additions & 13 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.2",
3+
"version": "0.3.3",
44
"main": "./dist/index.js",
55
"bin": {
66
"openapi-codegen": "./dist/sh.js"

src/generators/core/SchemaResolver.class.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,23 @@ export class SchemaResolver {
155155
}
156156

157157
private initializeSchemaTags() {
158-
const filterRefObjs = (objs?: unknown[]) => objs?.filter(isReferenceObject) ?? [];
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+
};
159175

160176
for (const path in this.openApiDoc.paths) {
161177
const pathItemObj = this.openApiDoc.paths[path] as OpenAPIV3.PathItemObject;
@@ -170,20 +186,17 @@ export class SchemaResolver {
170186

171187
// Collect all parameter objects that are references
172188
const schemaRefObjs = [] as OpenAPIV3.ReferenceObject[];
173-
schemaRefObjs.push(
174-
...filterRefObjs(operation.parameters?.map((param) => (param as OpenAPIV3.ParameterObject).schema)),
175-
);
189+
operation.parameters?.map((param) => {
190+
schemaRefObjs.push(...getSchemaRefs((param as OpenAPIV3.ParameterObject).schema));
191+
});
176192

177193
// Collect all requestBody objects that are references
178194
if (operation.requestBody) {
179195
const requestBodyObj = this.resolveObject(operation.requestBody);
180196
const mediaTypes = Object.keys(requestBodyObj.content ?? {});
181197
const matchingMediaType = mediaTypes.find(isParamMediaTypeAllowed);
182198
if (matchingMediaType) {
183-
const schema = requestBodyObj.content?.[matchingMediaType]?.schema;
184-
if (isReferenceObject(schema)) {
185-
schemaRefObjs.push(schema);
186-
}
199+
schemaRefObjs.push(...getSchemaRefs(requestBodyObj.content?.[matchingMediaType]?.schema));
187200
}
188201
}
189202

@@ -193,10 +206,7 @@ export class SchemaResolver {
193206
const mediaTypes = Object.keys(responseObj.content ?? {});
194207
const matchingMediaType = mediaTypes.find(isMediaTypeAllowed);
195208
if (matchingMediaType) {
196-
const schema = responseObj.content?.[matchingMediaType]?.schema;
197-
if (isReferenceObject(schema)) {
198-
schemaRefObjs.push(schema);
199-
}
209+
schemaRefObjs.push(...getSchemaRefs(responseObj.content?.[matchingMediaType]?.schema));
200210
}
201211
}
202212

0 commit comments

Comments
 (0)