Skip to content

[kotlin] enforce fields in data classes #19526

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

Conversation

jaqxues
Copy link
Contributor

@jaqxues jaqxues commented Sep 4, 2024

Fix #14710

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.6.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.

@jaqxues
Copy link
Contributor Author

jaqxues commented Sep 4, 2024

Can anyone of you review quickly? Thanks!
(Random subset of 3, not sure if I should've tagged less or more)

Note: Maybe not using class at all but just "object" would be a good idea. Your call though

@dr4ke616 @stefankoppier @yutaka0m

@wing328
Copy link
Member

wing328 commented Sep 9, 2024

can you please follow step 3 to update the samples so that the CI can test/verify the change?

cc @dr4ke616 (2018/08) @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06)

@jaqxues
Copy link
Contributor Author

jaqxues commented Sep 9, 2024

@wing328 I ran the 3 commands when I made the commit already, and had no additional changes. Reran again now, git status remains clean.

Unless I should add additional things, I would say step 3 is handled.

@wing328
Copy link
Member

wing328 commented Sep 10, 2024

you're right. samples are up-to-date

one thing i don't understand is how come there's no change in the samples as there are more test models with properties

i'll take another look later this week and run some tests

did you test the fix locally? I assume it works for your use cases

@jaqxues
Copy link
Contributor Author

jaqxues commented Sep 13, 2024

It does work.

Out of interest, what are you expecting to change? If there were a sample about empty data, someone would probably have noticed that it generates code that does not compile. (I am new to this project and do not have any profound knowledge about the CI setup etc)

@@ -74,7 +74,7 @@ import {{packageName}}.infrastructure.ITransformForStorage
@Deprecated(message = "This schema is deprecated.")
{{/isDeprecated}}
{{>additionalModelTypeAnnotations}}
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#discriminator}}interface{{/discriminator}}{{^discriminator}}data class{{/discriminator}} {{classname}}{{^discriminator}} (
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#discriminator}}interface{{/discriminator}}{{^discriminator}}{{#hasVars}}data {{/hasVars}}class{{/discriminator}} {{classname}}{{^discriminator}} (
Copy link
Member

Choose a reason for hiding this comment

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

@jaqxues the hasVars is true when the model has at least one property.

what I don't understand is there's no change to the samples (e.g. kotlin petstore client) given that the test petstore pet has at least one model with properties.

Copy link
Contributor Author

@jaqxues jaqxues Sep 28, 2024

Choose a reason for hiding this comment

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

This fixes an issue with models without properties, so the opposite case. A data class in kotlin is not allowed to have no properties. So this compilation error is fixed by not making it data class in that special case. But I am not sure if it makes sense to change the class to object even in that special case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the clarification 👍

@4brunu 4brunu merged commit af2ed1c into OpenAPITools:master Sep 30, 2024
15 checks passed
@wing328 wing328 added this to the 7.9.0 milestone Oct 7, 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.

[BUG] [Kotlin] Data class must have at least one primary constructor parameter
3 participants