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

[PJL-10221] handle some edge cases in OpenAPI v3.1 swagger generation #319

Merged
merged 6 commits into from
Oct 10, 2024

Conversation

dekim24
Copy link
Contributor

@dekim24 dekim24 commented Oct 2, 2024

Change 1

Right now, ContainsOnly validators are being handled identically to OneOf validators. This is because, in Marshmallow, ContainsOnly is a subclass of OneOf. However, this isn't actually correct because the logic is different -

  • ContainsOnly: Validator which succeeds if value is a sequence and each element in the sequence is also in the sequence passed as choices.
  • OneOf: Validator which succeeds if value is a member of choices.

I added a separate validator to handle ContainsOnly and validated that it produces the expected output against one of our repos (PADD). You can see in the below screenshot that we were incorrectly specifying the enum: parameter at the level of the value itself (which causes an error if you run this against an OpenAPI validator, because you're comparing an array to a string) when the enum: should actually be at under the items:.

Screenshot 2024-10-02 at 8 58 13

Change 2

In OpenAPI 3 https://swagger.io/docs/specification/v3_0/data-models/dictionaries/ we are able to specify the allowed types of dictionary values (keys must always be strings so there is no need to specify their type) in the swagger file. The corresponding marshmallow param would be values. I added logic to factor in any schema passed into the values param and generate it in the swagger file. I also tested this on PADD and got expected results.

@dekim24 dekim24 requested a review from a team as a code owner October 2, 2024 15:57
@dekim24 dekim24 requested a review from auramix October 2, 2024 15:57
def get_items(
self, obj: ContainsOnly, context: _Context
) -> Union[Type[UNSET], m.fields.List]:
return {"type": context.memo["items"]["type"], "enum": obj.choices}
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 messy but I couldn't figure out a better way to figure out the type of the parent value when the obj we are passed is the marshmallow.validator i.e. 1 level inside the value.

Copy link
Contributor

@auramix auramix left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this bug and contributing to flask-rebar, Eugene. Test cases look good-- happy to approve this.

I will add a link to this PR in the open issue from 2020 and mark it as closed: #183

EDIT: Actually, this is looking like a 2-for-1 : #310 🙌 🎉

@dekim24 dekim24 merged commit c9edc64 into master Oct 10, 2024
82 checks passed
@dekim24 dekim24 deleted the pjl-10221 branch October 10, 2024 14:12
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.

2 participants