-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[Dart-Dio-Next]#9082 Add json_serializable serialization option #9980
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-Dio-Next]#9082 Add json_serializable serialization option #9980
Conversation
|
Nice job! Was hoping someone gets the ball rolling on this and prepared the codebase somewhat. |
|
I am also very interested in this. I would love to test it. Migrating our project to built_value would be super inconvenient. |
|
I think this should be new generator extendin dartdio.java not a config flag on dio, it's a big change in generator and there's a lot of potential for generator conflicts and bugs in generated code if they life together. If you decide to keep them together under one generator at least add tests for dio-json_serialisabable |
| strict-raw-types: true | ||
| strong-mode: | ||
| implicit-dynamic: false | ||
| implicit-dynamic: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no sure that's wanted, why changing this ? if it's a requirement for the lib please do it only for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry this wasn't meant to be committed. The code generated by json_serializable includes a lot of implicit dynamic and so this option throws up a lot of errors in the generated code.
How do we feel about excluding *.g.dart instead, so only the generated code is excluded? Building on that, only exclude generated code if we're using json_serializable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to me doesn't make sens to analyze generated code, so I'm fine to exclude *.g.dart for all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure you can exclude files from strong-mode. I thought this only works for the lints. But if it works then go for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to work? google/json_serializable.dart#557 (comment)
modules/openapi-generator/src/main/resources/dart/libraries/dio/build.yaml.mustache
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/dart2/pubspec.mustache
Outdated
Show resolved
Hide resolved
We will not make a new generator each time we want to change the serialization library. It doesn't make sens and will be a nightmare to maintain. It need to be done properly to avoid conflicts and bugs, here it's on different files for the most parts so it's fine. But sure it would be better with tests. I'm interested to use freezed at some point so I really think the config flag is the way to go to centralize all the Dio related stuff. |
I disagree, we should not start a new generator. We end up with no one maintaining it. I prepared the dart-dio-next generator exactly for this use case. And yes we need a sample and tests :) |
|
Hey thanks for the feedback so far. Just to give an indication of where I'm at with this. We spent most of last week migrating our mobile app from Swagger/codegen to this branch of Dart-Dio-Next. So far all appears to have worked well with no bugs introduced to our app. The mobile app is scheduled for a release next week, and so this week is mostly internal testing and QA. So far though we're happy with the code this branch is generating. Once our app update has been released I'll have a bit of time to come back and tidy up this PR. Agreed tests and samples are needed to ensure both this works, and that it doesn't break the built_value serialisation. I pushed this PR early as a draft just so people can be aware it's happening and to provide early feedback. Cheers |
|
Hey @MichaelMarner - I had some time so I had a go at adding a sample project and some tests to your changes. It uncovered a couple of issues in the templates which I've addressed wherever possible however I'm unsure how to progress it any further as I'm not familiar enough with this Java/OpenAPI/maven to wire up the automated testing (although I gave it a go based on the existing projects). Happy to keep pushing it forward if I can get some feedback on it. |
|
Good work, i'lll try to checkout the sample and see if I can help. One thing I noticed is the use of mutable models. I would like to see immutable models by default and if anyone requires mutable models, we could add a flag for it. Can you merge master when you get a chance? |
|
Thanks @kuhnroyal - merged master into my branch but makes the PR into Michael's fork pretty noisy so might be better to just rebase and cherry pick once happy with it. |
|
Hey so it turns out that real work kind of took priority over this PR, but I've just made a few changes to fix some things up:
|
|
Hi, I'm using a version before these latest commits and was relying on mutable models (when sending back to the rest API for creation/update). I'm quite new with dart, is there a different way I should be doing this? ... or could properties be made optionally final? |
|
Hi @elliots all properties are named parameters in the model constructor, so you can set them that way: new MyModel(property1: "asdf", property2: "etc");I can look at adding an option for optional |
|
I'm updating individual properties on the model as the user performs actions, if they are final I would need a second model for the ui that I then use to create the values to post back to the server. Worth adding an option if you think others would do similar, but I've forked with the 'final' removed for my use so no worries. |
|
is this still active? how about adding https://pub.dev/packages/freezed |
|
We've been using this in our production app since August of last year without any issues. At this point I think it's ready to go but I'm not sure what the next steps are, as I'm new to this project |
...or/src/main/resources/dart/libraries/dio/serialization/json_serializable/build.yaml.mustache
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs includeIfNull to be used in conjunction with required. If field is required, then null value must be included
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe I have addressed this now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enum parameter can have a value, so field FIELD_1 can be represented by field-1 and so field-1 must be mapped to FIELD_1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This supported from 4.2.0 https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum/fieldRename.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latest commits have fixed up generation of enums, and I've used @JsonValue() to specify the enum values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same mapping here
|
Please add sample project for this generator. |
|
Please target current master, which will be released as v6.0.0. By the way, shall we replace cc @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08) |
Enum fixes and Maven module
Have merged your PR back into this |
|
I'll check later for the failures, must have a typo in the POM. |
* add a beta hint to json_serializable option
…-json-serializable
|
Is this not a good time to add an option for freezed as well? |
If you write it sure :) |
I think it might be a bit of scope creep to also introduce freezed to this PR. I'd really like to see this merged so I don't have to keep rebasing against master as new changes come in. It will be easy enough and cleaner to extend this branch to include freezed as an option later on. My 2c anyway. |
|
Yea definitely not as part of this PR. |
Update docs and fix wrong maven module
|
Needs a merge again |
|
@MichaelMarner Can you merge again please, want to get this in |
c28c5b3 to
554a63e
Compare
554a63e to
67ec4ed
Compare
3066c2e to
a016b88
Compare
|
Sorry, was a long weekend in South Australia and I was out of town. Have merged
|
1266195 to
cf5ceef
Compare
modules/openapi-generator/src/main/resources/dart/libraries/dio/pubspec.mustache
Show resolved
Hide resolved
|
Big thanks @MichaelMarner ! 🤘 |
|
Woo! Thanks for your patience everyone. |
This is a WIP/PoC to add
json_serializableas an alternative tobuilt_valuein the Dart-Dio-Next generator.For context: We would like to migrate our Flutter app to full null safety, and so we are migrating from the old Dart generator to Dart-Dio-Next.
We have found that moving from models with to/fromJson methods to using built_value has made the job of migrating bigger and riskier than we'd like. We need to rewrite large chunks of our existing persistence layer and other parts.
Adding
json_serializableas an option has mostly involved reworking existing templates in the old Dart generator to generate null safe code.Fixes #9082
@swipesight @jaumard @josh-burton @amondnet @sbu-WBT @kuhnroyal @agilob
PR checklist
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.
master,5.3.x,6.0.x