Skip to content

Commit f552dd2

Browse files
committed
fix: include to required models which reference from models
1 parent b56e7fd commit f552dd2

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/generateCustomSpec.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,31 @@ type Config = Options & {
1515
};
1616

1717
export const generateCustomSpec = async (config: Config) => {
18+
const getNameFromRef = (ref: string): string => {
19+
return ref.split('/').slice(-1)[0];
20+
};
21+
1822
const getSchemaRefFromContent = (content: OpenApiMediaType): string => {
1923
let ref: string = '';
2024

2125
ref = content.$ref || content.schema?.$ref || content.schema?.items?.$ref || '';
2226

23-
return ref.split('/').slice(-1)[0];
27+
return getNameFromRef(ref);
28+
};
29+
30+
const recursiveAddAllUnknownModels = (modelName: string): void => {
31+
const model = list.components?.schemas ? list.components.schemas[modelName] : undefined;
32+
if (model === undefined) return;
33+
34+
for (const property in model.properties) {
35+
const ref = model.properties[property].$ref || model.properties[property].items?.$ref || '';
36+
const modelName = getNameFromRef(ref);
37+
38+
if (!requiredSchemasSet.has(modelName)) {
39+
requiredSchemasSet.add(modelName);
40+
recursiveAddAllUnknownModels(modelName);
41+
}
42+
}
2443
};
2544

2645
const list: OpenApi = await getOpenApiSpec(config.input);
@@ -57,9 +76,12 @@ export const generateCustomSpec = async (config: Config) => {
5776
if (!('url' in requestMethodData)) {
5877
if ('parameters' in requestMethodData) {
5978
// add schemas from {apiPath}/{method}/parameters
60-
requestMethodData.parameters?.forEach(parameter =>
61-
requiredSchemasSet.add(getSchemaRefFromContent(parameter))
62-
);
79+
requestMethodData.parameters?.forEach(parameter => {
80+
const modelName = getSchemaRefFromContent(parameter);
81+
82+
requiredSchemasSet.add(modelName);
83+
recursiveAddAllUnknownModels(modelName);
84+
});
6385
}
6486
if ('responses' in requestMethodData) {
6587
const responsesCodeData = Object.values(requestMethodData.responses);
@@ -69,7 +91,10 @@ export const generateCustomSpec = async (config: Config) => {
6991

7092
// add schemas from {apiPath}/{method}/responses/{responseType}/content
7193
contentTypeData.forEach(content => {
94+
const modelName = getSchemaRefFromContent(content);
95+
7296
requiredSchemasSet.add(getSchemaRefFromContent(content));
97+
recursiveAddAllUnknownModels(modelName);
7398
});
7499
});
75100
}

0 commit comments

Comments
 (0)