Skip to content
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

Fix discriminator propertyName inference #1342

Merged
merged 3 commits into from
Sep 13, 2023
Merged

Fix discriminator propertyName inference #1342

merged 3 commits into from
Sep 13, 2023

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Sep 13, 2023

Changes

Fixes an issue where propertyName wasn’t automatically generated for discriminators (mappings are not required; they only are needed to override the automatic inference).

Related to #1158 and #1116

How to Review

  • Tests added; tests should pass.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Sep 13, 2023

🦋 Changeset detected

Latest commit: 8169953

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

This PR includes changesets to release 1 package
Name Type
openapi-typescript Patch

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

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Sep 13, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8169953
Status: ✅  Deploy successful!
Preview URL: https://f71c24a6.openapi-ts.pages.dev
Branch Preview URL: https://disc.openapi-ts.pages.dev

View logs

@drwpow drwpow changed the title Fix disciminator propertyName inference Fix discriminator propertyName inference Sep 13, 2023
// core type: properties + additionalProperties
const coreType: string[] = [];

// discriminators
for (const k of ["oneOf", "allOf", "anyOf"] as ("oneOf" | "allOf" | "anyOf")[]) {
Copy link
Contributor Author

@drwpow drwpow Sep 13, 2023

Choose a reason for hiding this comment

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

Note: this was moved up before property generation due to the change in automatic discriminator.propertyName possibly adding keys to the object. We can’t detect if there may be a key conflict, but we can let the explicit properties override it if they come afterward.

if (Array.isArray(node.oneOf)) {
for (const child of node.oneOf) {
if (!child || typeof child !== "object" || !("$ref" in child)) continue;
oneOf.push(child.$ref);
Copy link
Contributor Author

@drwpow drwpow Sep 13, 2023

Choose a reason for hiding this comment

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

This adds automatic inference from parent oneOf types, but unfortunately the mapping is brittle (probably breaks for remote schemas).

I think the only way to fix this is if this library pre-bundled schemas via something like Redocly bundle

@drwpow drwpow merged commit c17669d into main Sep 13, 2023
8 checks passed
@drwpow drwpow deleted the disc branch September 13, 2023 17:58
@github-actions github-actions bot mentioned this pull request Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant