-
-
Couldn't load subscription status.
- Fork 414
fix: union when there are multiple request/response types defined #1462
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "swagger-typescript-api": minor | ||
| --- | ||
|
|
||
| Add support for multiple request/response types to be defined as unions |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -331,16 +331,55 @@ export class SchemaRoutes { | |||||||||||||||||||
|
|
||||||||||||||||||||
| /* content: { "multipart/form-data": { schema: {...} }, "application/json": { schema: {...} } } */ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /* for example: dataType = "multipart/form-data" */ | ||||||||||||||||||||
| const contentTypes = Object.keys(content); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // if there's only one content type, return it | ||||||||||||||||||||
| if (contentTypes.length === 1 && content[contentTypes[0]]?.schema) { | ||||||||||||||||||||
| return { | ||||||||||||||||||||
| ...content[contentTypes[0]].schema, | ||||||||||||||||||||
| dataType: contentTypes[0], | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Check if there are multiple media types with schemas | ||||||||||||||||||||
| const schemasWithDataTypes = []; | ||||||||||||||||||||
| for (const dataType in content) { | ||||||||||||||||||||
| if (content[dataType]?.schema) { | ||||||||||||||||||||
| return { | ||||||||||||||||||||
| schemasWithDataTypes.push({ | ||||||||||||||||||||
| ...content[dataType].schema, | ||||||||||||||||||||
| dataType, | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // If there's only one schema, return it directly | ||||||||||||||||||||
| if (schemasWithDataTypes.length === 1) { | ||||||||||||||||||||
| return schemasWithDataTypes[0]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // If there are multiple schemas with different structures, create a oneOf schema to generate a union type | ||||||||||||||||||||
| if (schemasWithDataTypes.length > 1) { | ||||||||||||||||||||
| // Check if all schemas are structurally the same | ||||||||||||||||||||
| // If they are, just return the first one | ||||||||||||||||||||
| const firstSchema = schemasWithDataTypes[0]; | ||||||||||||||||||||
| const allSchemasAreSame = schemasWithDataTypes.every((schema) => | ||||||||||||||||||||
| lodash.isEqual( | ||||||||||||||||||||
| lodash.omit(schema, "dataType"), | ||||||||||||||||||||
| lodash.omit(firstSchema, "dataType"), | ||||||||||||||||||||
|
Comment on lines
+365
to
+368
|
||||||||||||||||||||
| const allSchemasAreSame = schemasWithDataTypes.every((schema) => | |
| lodash.isEqual( | |
| lodash.omit(schema, "dataType"), | |
| lodash.omit(firstSchema, "dataType"), | |
| const firstSchemaOmitted = lodash.omit(firstSchema, "dataType"); | |
| const allSchemasAreSame = schemasWithDataTypes.every((schema) => | |
| lodash.isEqual( | |
| lodash.omit(schema, "dataType"), | |
| firstSchemaOmitted, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling single content types is duplicated: lines 337-342 handle the case where
contentTypes.length === 1, and lines 356-358 handle the case whereschemasWithDataTypes.length === 1. These blocks will always produce the same result when there's only one schema. The first check (lines 337-342) should be removed since the loop and subsequent check (lines 346-358) already handle this case correctly.