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

[BUG][GO] Include imports for fields when using polymorphism #19958

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

perhallgren
Copy link

@perhallgren perhallgren commented Oct 23, 2024

The current implementation treats anyOf and oneOf slightly differently than allOf. The intention is clear from the comment "the model is no model, but is a anyOf or oneOf". However, the generator does include all properties and just makes this distinction when dealing with imports. So one would only see the issue if the property on the parent also requires importing something, e.g. it is a datetime and requires import time.

With this update we instead treat all the polymorphism features the same way when dealing with imports.

Testing

Tested separately using https://gist.github.com/perhallgren/d2af2441d7c265a06407313b064f68d6 and also using the 3GPP standard as inputs.

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.

Discussion

What's the correct way to read the specs?

I didn't find the specs very explicit on if it is valid to use properties on a model that uses anyOf/oneOf polymorphism. allOf is pretty straight forward, like normal OOP inheritence - the Dog extends Pet. oneOf/anyOf are applied in the parent model instead of the child, and intuitively if a Pet is oneOf Dog or Cat it doesn't make sense that it has properties that doesn't exist on either dog or cat.

I think the spec gives the impression that the discriminator property should be dealt with in the same way for all of them. It also says this:

In both the oneOf and anyOf use cases, all possible schemas MUST be listed explicitly. To avoid redundancy, the discriminator MAY be added to a parent schema definition, and all schemas comprising the parent schema in an allOf construct may be used as an alternate schema.

Which actually doesn't spell out that you can or cannot add the discriminator in the parent object also for oneOf and anyOf. But since that would make everyone's life easier, users will refrain from copy-pasting and using the parent object. An example where this was expected to be possible can be found in #16016.

Usage in the wild

The 3GPP 5G standard is using anyOf on models that also has properties defined. There, it is expected that even if a model has an anyOf/oneOf, the properties specified on the model are treated as for any other model.

`anyOf`/`allOf`/`oneOf` should not hide the models' properties.

Currently only `allOf` is treated as an inheritence, while `anyOf` and
`oneOf` are essentially replaced by the referenced model.
Got updated from running `./mvnw clean install -DskipTests -Dmaven.javadoc.skip=true`.
@perhallgren
Copy link
Author

Pings for technical committee: @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5

@perhallgren
Copy link
Author

Tried to see how other generators implement this.

The python generator gives special treatment to anyOf/onOf, but the Java generator looks like it treats anyOf/oneOf/allOf equally .

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