Skip to content

Stop generating an undocumented case for enums/oneOfs #204

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

Closed
czechboy0 opened this issue Aug 16, 2023 · 1 comment · Fixed by #205
Closed

Stop generating an undocumented case for enums/oneOfs #204

czechboy0 opened this issue Aug 16, 2023 · 1 comment · Fixed by #205
Assignees
Labels
area/openapi Adding/updating a feature defined in OpenAPI. kind/bug Feature doesn't work as expected.

Comments

@czechboy0
Copy link
Contributor

czechboy0 commented Aug 16, 2023

Consider not generating an undocumented case for enums/oneOfs by default.

It seemed like a good idea to generate the undocumented case before, as it allows for API evolution - as adding an enum or oneOf case maybe shouldn't be a breaking change.

However, OpenAPI/JSON Schema enums/oneOfs are closed by default, so we're actually diverging from the specification here.

And it's causing issues when these enums/oneOfs are nested within other allOfs/oneOfs/anyOfs and it "successfully" parses even though it parses into an undocumented case.

Talk about this more, but we might need to undo this, and follow the specification closely, and also generate closed enums by default. Possibly it could be an option to generate an undocumented case? Or we should document that open enums can be represented as:

MyOpenEnum:
  anyOf:
    - type: string
      enum:
        - foo
        - bar
        - baz
    - type: string

And an open oneOf with:

MyOpenOneOf:
  anyOf:
    - oneOf:
        - #/components/schemas/Foo
        - #/components/schemas/Bar
        - #/components/schemas/Baz
    - {}

And as an adopter, you first check the first schema if it's not nil. If it's not nil, the closed enum/oneOf parsed successfully. If the first element is nil, the second will be filled in, which is the fallback value.

@czechboy0 czechboy0 added area/openapi Adding/updating a feature defined in OpenAPI. kind/bug Feature doesn't work as expected. labels Aug 16, 2023
@czechboy0
Copy link
Contributor Author

It would simplify the composition of types to be able to stop generating open enums/oneOfs, especially since adopters have a pattern to work around it. Plus, we're not specification-compliant now. So we should probably remove this.

@czechboy0 czechboy0 changed the title Consider not generating an undocumented case for enums/oneOfs by default Consider not generating an undocumented case for enums/oneOfs Aug 16, 2023
@czechboy0 czechboy0 changed the title Consider not generating an undocumented case for enums/oneOfs Reconsider generating an undocumented case for enums/oneOfs Aug 16, 2023
@czechboy0 czechboy0 changed the title Reconsider generating an undocumented case for enums/oneOfs Stop generating an undocumented case for enums/oneOfs Aug 16, 2023
@czechboy0 czechboy0 self-assigned this Aug 16, 2023
czechboy0 added a commit that referenced this issue Aug 17, 2023
### Motivation

Fixes #204.

Depends on apple/swift-openapi-runtime#40.

### Modifications

This introduces the feature flag `closedEnumsAndOneOfs` which stops
generating the `undocumented` case for enums and oneOfs (no other places
are affected, such as responses, and so on.). The default behavior in
0.1.x stays the same, but in 0.2.0 we'll stop generating `undocumented`
cases unconditionally.

This fixes our divergence from the OpenAPI specification, as we treated
enums and oneOfs as open, but they're closed by default.

I also added docs on this pattern to make it easier for adopters to know
how to create open enums and oneOfs on purpose.

### Result

We get closer to being spec-compliant.

### Test Plan

Created new snippet tests to verify the new functionality, will adapt
file-based tests when we're preparing 0.2.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/openapi Adding/updating a feature defined in OpenAPI. kind/bug Feature doesn't work as expected.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant