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

[dart][dart-dio] Nullable support/improvements #8727

Merged
merged 7 commits into from
Feb 17, 2021

Conversation

kuhnroyal
Copy link
Contributor

Follow-up for #8235 and a requirement for any move towards Dart NNBD in #8278
Fixes #5411

dart-dio/built_value

  • Disable all fields nullable by default:
    This is not in line with OAS and will prevent future Dart nullabilty features (NNBD) from being useful as all types would be optional. This is a breaking change with fallback (set nullableFields=true ).

  • Properties are @nullable when nullable OR ( not required AND no defaultValue )

  • Support nullable/required fields in JSON

    • properties in built_value need to be @nullable when they are nullable in OAS or when they are not required in OAS
    • built_value does not support serializing null values by default as it is based on a serialization format based on iterables/lists and not maps
    • dart-dio uses the built_value json plugin to convert the built_value format to regular json
    • by generating a custom serializer for each class we can add support for serializing null values if the property is required AND nullable in OAS

Dart

  • basically just serialize null when the field is required and nullable

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.

CC @swipesight (2018/09) @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12)

This is not in line with the OAS and will prevent future Dart nullabilty features (NNBD) from being useful as all types would be optional.
Users can still opt-in for this.
* properties in built_value need to be nullable when they are nullable in OAS or when they are not required in OAS
* built_value does not support serializing `null` values by default as it is based on a serialization format based on iterables/lists and not maps
* dart-dio uses the built_value json plugin to convert the built_value format to regular json
* by generating a custom serializer for each class we can add support for serializing `null` values if the property is required AND nullable in OAS
* this is a breaking change as not all properties in the models are nullable by default anymore
@auto-labeler
Copy link

auto-labeler bot commented Feb 16, 2021

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@@ -40,7 +40,7 @@

private static final String CLIENT_NAME = "clientName";

private boolean nullableFields = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think to move this to dart generator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would rather deprecate this and get rid of it in 6.0 - we should not promote this going forward in with Dart/NNBD. I think this was more of a workaround for built_value. Maybe @josh-burton can say more to the original motivation.

@agilob
Copy link
Contributor

agilob commented Feb 16, 2021

@wing328 is there any other generator that supports not-null-by-default?

@kuhnroyal Most languages are null-by-default, can this change potentially make dart-dio incompatible wither other generators without changes to spec file? I thought adding NNBD would be a bit simpler to start with, by just changing
int val into int? val if no default value is specified. What's your plan for next changes?

@kuhnroyal
Copy link
Contributor Author

@agilob Kotlin is NNBD and basically the same as Dart in this regard. I have looked at the templates there and basically mirrored this with some adaptions for built_value.

Basically everything that is nullable in OAS needs to be an optional type in Dart. Everything else should be non-null. Doing this with the current mutable models in the dart2 generator isn't great but possible, it should be straight forward for dart-dio.

@kuhnroyal
Copy link
Contributor Author

I would add a generator NNBD switch that defaults to true for 5.1.0 and allow users to set this to false.

@wing328
Copy link
Member

wing328 commented Feb 17, 2021

@wing328 is there any other generator that supports not-null-by-default?

csharp-netcore client generator comes to my mind

@wing328 wing328 added this to the 5.1.0 milestone Feb 17, 2021
@wing328 wing328 merged commit c916df9 into OpenAPITools:master Feb 17, 2021
@kuhnroyal kuhnroyal deleted the dart-dio/nullable-improvements branch February 17, 2021 13:45
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][dart][dart-dio] required fields are nullable in generated files
3 participants