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

Use Kotlin nullable operator for nullable properties #9799

Closed

Conversation

josh-bridge
Copy link
Contributor

@josh-bridge josh-bridge commented Jun 17, 2021

The previous check of isReadOnly was not applicable to whether a value should be null or not. As far as I can tell that is more to do with a RESTful design principal than to do with whether the code should present a models values as nullable.
Initially I thought the ? should be removed if the value is set to required = true but it is important to note that required != non-null, i.e. the field can be required but the value might still be null (if nullable = true).

The default null value will no longer be supplied if the value is not nullable.

Also removed escaping of default string values (this would cause any default string value to be written to the file as
val test = "value" instead of val test = "value".

This would clearly be a breaking change so have set it to a major release branch. Please let me know if you think it could actually go into a minor release.

[kotlin committee]
@jimschubert @dr4ke616 @karismann @Zomzog @andrewemery @4brunu @yutaka0m

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/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Joshua Bridge added 2 commits June 17, 2021 19:38
The previous check of isReadOnly was not applicable to whether a value should be null or not. Also removed the default null value if the value is not nullable.

Also removed escaping of default values (this would cause any string value to be written to the file as val test = "value" instead of val test = "value"
@josh-bridge josh-bridge changed the title Kotlin nullable fields Use Kotlin nullable operator for nullable properties Jun 17, 2021
@kuFEAR
Copy link
Contributor

kuFEAR commented Jun 25, 2021

Why do all properties become required in your samples and without default '= null' ? If you look on kotlinx.serialization you will see that if the field is not in model(empty field, not null) it crashes on runtime.

@josh-bridge
Copy link
Contributor Author

josh-bridge commented Aug 3, 2021

Why do all properties become required in your samples and without default '= null' ? If you look on kotlinx.serialization you will see that if the field is not in model(empty field, not null) it crashes on runtime.

The default value of the nullable option in the OpenAPI schema is false, as shown below
https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schemaNullable

If the value by default should not be nullable, then the fields themselves should also not be nullable right? And to your point about it crashing, maybe I need to update it to handle that, as the empty field is more to do with optional / required than nullable.

@kuFEAR
Copy link
Contributor

kuFEAR commented Aug 3, 2021

Kotlin/kotlinx.serialization#1196 In last version kotlinx.serialization has new flag explicitNulls for fixing this behaviour.
Now I use this generator with kotlinx.serialization and it generate val field: Type? = null properties if it "optional" and it works well, without crashes:

fun provideKotlinxSerialization(): Json = Json {
        registerCommonJvmAdapters()
        coerceInputValues = true
        ignoreUnknownKeys = true
        isLenient = true
    }

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