Skip to content

Commit ac9420c

Browse files
committed
Preserver order from selectedSchemas
1 parent 89ad739 commit ac9420c

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

packages/react-openapi/src/schemas/resolveOpenAPISchemas.test.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('#resolveOpenAPISchemas', () => {
2121
const filesystem = await fetchFilesystem(
2222
'https://petstore3.swagger.io/api/v3/openapi.json'
2323
);
24-
const resolved = await resolveOpenAPISchemas(filesystem, { schemas: ['Pet'] });
24+
const resolved = await resolveOpenAPISchemas(filesystem, { schemas: ['Pet', 'Tag'] });
2525

2626
expect(resolved).toMatchObject({
2727
schemas: [
@@ -47,6 +47,24 @@ describe('#resolveOpenAPISchemas', () => {
4747
},
4848
},
4949
},
50+
{
51+
name: 'Tag',
52+
schema: {
53+
type: 'object',
54+
properties: {
55+
id: {
56+
type: 'integer',
57+
format: 'int64',
58+
},
59+
name: {
60+
type: 'string',
61+
},
62+
},
63+
xml: {
64+
name: 'tag',
65+
},
66+
},
67+
},
5068
],
5169
});
5270
});

packages/react-openapi/src/schemas/resolveOpenAPISchemas.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,26 @@ export async function resolveOpenAPISchemas(
3232

3333
return { schemas };
3434
}
35-
3635
/**
3736
* Get OpenAPI components.schemas that are not ignored.
3837
*/
3938
function getOpenAPIComponents(
4039
schema: OpenAPIV3.Document | OpenAPIV3_1.Document,
4140
selectedSchemas: string[]
4241
): OpenAPISchema[] {
43-
const schemas = schema.components?.schemas ?? {};
44-
return Object.entries(schemas)
45-
.filter(([key]) => selectedSchemas.includes(key))
46-
.filter(([, schema]) => !shouldIgnoreEntity(schema))
47-
.map(([key, schema]) => ({ name: key, schema }));
42+
const componentsSchemas = schema.components?.schemas ?? {};
43+
44+
// Preserve the order of the selected schemas
45+
return selectedSchemas
46+
.map((name) => {
47+
const schema = componentsSchemas[name];
48+
if (schema && !shouldIgnoreEntity(schema)) {
49+
return {
50+
name,
51+
schema,
52+
};
53+
}
54+
return null;
55+
})
56+
.filter((schema): schema is OpenAPISchema => !!schema);
4857
}

0 commit comments

Comments
 (0)