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

python-pydantic-v1: Keep trailing commas for enum validation tuples #19985

Merged

Conversation

chiragjn
Copy link
Contributor

@chiragjn chiragjn commented Oct 28, 2024

It can happen that sometimes a enum just has a single member

This can result in a pydantic validator that can be incorrect
E.g.

class TestClass(BaseModel):
    type: StrictStr = Field(...)  # Say this can only take a single value like "test" 

    @validator('type')
    def type_validate_enum(cls, value):
        if value not in ("test"):
            raise ValueError(...)

If I do some substring of "test", e.g. TestClass(type="tes") this validation will not fail

Then trailing , in this case is required

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328
Copy link
Member

wing328 commented Oct 28, 2024

thanks for the PR

can you please add a test schema (enum with a single item) in modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml ?

@chiragjn
Copy link
Contributor Author

Tagging @multani for early feedback. Will work on adding small tests

@chiragjn chiragjn changed the title python-pydantic-v1: Keep trailing commas for tuples when enum has just single member python-pydantic-v1: Keep trailing commas for enum validation tuples Oct 28, 2024
@chiragjn
Copy link
Contributor Author

Updated samples and added a test

@chiragjn chiragjn force-pushed the cj_fix_single_enum_member_validation branch from a1a1537 to d679874 Compare October 28, 2024 08:17
@chiragjn
Copy link
Contributor Author

Tagging other members of Python committee
@cbornet @tomplus @krjakbrjak @fa0311

@multani
Copy link
Contributor

multani commented Oct 30, 2024

@chiragjn did you consider replacing the tuples by lists instead?

Semantically, I think it would be more "correct", and syntax-wise it would also prevent this kind of problem.

@chiragjn
Copy link
Contributor Author

Hmm, sure I don't mind doing that
Thought tuples are faster to allocate hence didn't change
Let me know

@multani
Copy link
Contributor

multani commented Oct 31, 2024

I'm not sure if that makes such a difference ; if this was a performance problem too, they should be created only once anyway as they are all constants in the end.

We can merge this as it is though.

Copy link
Contributor

@multani multani left a comment

Choose a reason for hiding this comment

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

lgtm

@wing328 any preference between tuples and lists? Lists would have prevented this bug in the first place, at the expense of a slight performance penalty.

@wing328
Copy link
Member

wing328 commented Nov 2, 2024

to me, this is more like an edge case so my take is to use tuple instead.

@wing328 wing328 merged commit 67af02c into OpenAPITools:master Nov 2, 2024
42 checks passed
@wing328 wing328 added this to the 7.10.0 milestone Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants