Skip to content

fix: union when there are multiple request/response types defined#1462

Open
stefan-cooper wants to merge 4 commits into
acacode:mainfrom
stefan-cooper:314/fix
Open

fix: union when there are multiple request/response types defined#1462
stefan-cooper wants to merge 4 commits into
acacode:mainfrom
stefan-cooper:314/fix

Conversation

@stefan-cooper

Copy link
Copy Markdown

Contributes to #314

Signed-off-by: Stefan Cooper stefan.cooper27@gmail.com

Contributes to acacode#314

Signed-off-by: Stefan Cooper <stefan.cooper27@gmail.com>
@changeset-bot

changeset-bot Bot commented Oct 22, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 25b2de2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
swagger-typescript-api Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Stefan Cooper added 3 commits October 22, 2025 11:40
Signed-off-by: Stefan Cooper <stefan.cooper27@gmail.com>
Signed-off-by: Stefan Cooper <stefan.cooper27@gmail.com>
Signed-off-by: Stefan Cooper <stefan.cooper27@gmail.com>
@stefan-cooper

Copy link
Copy Markdown
Author

@smorimoto I've had a stab at trying to fix this myself. LMK what you think!

@smorimoto

Copy link
Copy Markdown
Collaborator

@codex review

@smorimoto smorimoto added the bug Something isn't working label Oct 22, 2025
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@smorimoto

Copy link
Copy Markdown
Collaborator

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no bugs!


@smorimoto smorimoto requested a review from Copilot October 23, 2025 04:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for generating union types when multiple media types are defined in OpenAPI request or response bodies. Previously, only the first media type was used; now all distinct schemas are combined into a union type.

Key Changes:

  • Enhanced getSchemaFromRequestType to detect multiple media types and create oneOf schemas for union type generation
  • Added logic in getSchemaFromRequestType to check if schemas are structurally identical before creating unions
  • Updated request body type generation to handle oneOf schemas by creating TypeScript union types

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/schema-routes/schema-routes.ts Core logic to detect multiple media types and generate union types via oneOf schemas
tests/spec/multiple-media-types/schema.json Test OpenAPI schema with Cat and Dog schemas under different media types
tests/spec/multiple-media-types/index.spec.ts Test case verifying union type generation for multiple media types
tests/spec/multiple-media-types/snapshots/index.spec.ts.snap Snapshot showing generated TypeScript with Cat | Dog union type
tests/snapshots/simple.test.ts.snap Updated snapshots reflecting union type changes in existing tests
tests/snapshots/extended.test.ts.snap Updated snapshots reflecting union type changes in existing tests
.changeset/shaggy-otters-walk.md Changeset documenting the new feature as a minor version bump

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +334 to +358
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];
}

Copilot AI Oct 23, 2025

Copy link

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 where schemasWithDataTypes.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.

Copilot uses AI. Check for mistakes.
Comment on lines +365 to +368
const allSchemasAreSame = schemasWithDataTypes.every((schema) =>
lodash.isEqual(
lodash.omit(schema, "dataType"),
lodash.omit(firstSchema, "dataType"),

Copilot AI Oct 23, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison lodash.omit(schema, 'dataType') is executed repeatedly for every schema in the array, including for firstSchema which is omitted once per iteration. Consider computing lodash.omit(firstSchema, 'dataType') once before the every() call to avoid redundant operations.

Suggested change
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,

Copilot uses AI. Check for mistakes.
@js2me

js2me commented Mar 11, 2026

Copy link
Copy Markdown
Member

@stefan-cooper thanks for your MR!
Fix merge conflicts please and tag me, I will merge this MR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants