Skip to content

Improve handling of oneOf and enum #1236

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

Merged
merged 1 commit into from
Jul 21, 2023
Merged

Improve handling of oneOf and enum #1236

merged 1 commit into from
Jul 21, 2023

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Jul 21, 2023

Changes

Before, if oneOf or enum was present, we ignored the core type (for objects, at least). In this PR, we now create an intersection of the core type + oneOf/enum.

The reason for the enum handling was although unconventional, I did find usage of it in the GitHub API schemas. And I couldn’t find any explicit signal that this is invalid. So just for object types, we’ll treat enum == oneOf.

How to Review

Tests should pass.

Checklist

  • Unit tests updated
  • examples/ directory updated (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Jul 21, 2023

🦋 Changeset detected

Latest commit: 45a79cb

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 Jul 21, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 45a79cb
Status: ✅  Deploy successful!
Preview URL: https://92639452.openapi-ts.pages.dev
Branch Preview URL: https://oneof-enum.openapi-ts.pages.dev

View logs

@@ -13321,7 +13323,7 @@ export interface components {
* Organization ruleset conditions
* @description Conditions for an organization ruleset
*/
"org-ruleset-conditions": (components["schemas"]["repository-ruleset-conditions"] & components["schemas"]["repository-ruleset-conditions-repository-name-target"]) | (components["schemas"]["repository-ruleset-conditions"] & components["schemas"]["repository-ruleset-conditions-repository-id-target"]);
"org-ruleset-conditions": Record<string, never> & ((components["schemas"]["repository-ruleset-conditions"] & components["schemas"]["repository-ruleset-conditions-repository-name-target"]) | (components["schemas"]["repository-ruleset-conditions"] & components["schemas"]["repository-ruleset-conditions-repository-id-target"]));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fortunately the Record<string, never> doesn’t interfere with the type at all; it’s just an empty object that gets ignored

@@ -90333,15 +90335,97 @@ export interface operations {
};
requestBody: {
content: {
"application/json": OneOf<[{
"application/json": ({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a big improvement, and a scenario where maybe we should start preferring practice over theory

@@ -447,18 +447,21 @@ export type SchemaObject = {

export interface StringSubtype {
type: "string";
enum?: (string | ReferenceObject)[];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m not completely happy with the enum type changes; we may need to change these in the future. But it just made this change easier.

@drwpow drwpow merged commit 95a4c8c into main Jul 21, 2023
@drwpow drwpow deleted the oneof-enum branch July 21, 2023 18:15
@github-actions github-actions bot mentioned this pull request Jul 21, 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