-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
feat(dart): dart-next generator #18970
base: master
Are you sure you want to change the base?
Conversation
cc core team: @wing328 @jimschubert @cbornet @jmini @etherealjoy I made a non-breaking change in DefaultCodegen by adding a new method public void updateCodegenPropertyEnum(CodegenProperty var, CodegenModel model) |
cc dart committee for early review: |
…to feat/dart-next
…to feat/dart-next
I think this is a great effort yet a complicated approach to create a single solution that handles different cases. Simplicity over complex use cases.I understand we need to handle fields that are not defined in a json or xml. I assume the purpose of Dart approach: // easy to define null properties
final apple = Apple();
// easy to define an apple instance with both properties.
final apple = Apple(cultivar: 'Pink Lady', origin: 'Australia'); Proposed approach: // easy to define null properties
final apple = Apple();
// complicated approach to define an apple instance with both properties.
final apple = Apple(cultivar: UndefinedWrapper('Pink Lady'), origin: UndefinedWrapper('Australia')); Now imagine a scenario where you have to deal with an api where we have to pass a huge object in post request. While UndefinedWrapper might solve a problem, it may introduce a cognitive load when these instances are actually used. Take for example I want to check if an apple instance has a name. if(apple.cultivar.isDefined){
// do something
}
// instead of
if(apple.cultivar != null){
// do something
} The same can be achieved by simply exporting an extension on 'Object?` Following works on primitives as well. But nevertheless we can completely skip it and just use plain null check. extension Undefined on Object? {
bool get isDefined => this != null;
bool get isUndefined => this == null;
} Same comment on your proposal for a new form of enum. build_runner is not bad!The purpose of existing code generation for serialization is to keep the codebase readable and leave the obvious stuffs to the generator. I agree these can be often slow on a larger projects. But when we use an api client generator how often do we have to run the build_runner? Once or twice if the spec is not clear. But then it stays in the repository. With dart macros around the corner do we really need another completely new serialization deserialization logic. Perhaps its helpful for XML cases, which I can agree. But i strongly believe the whole build_runner for any form of serialization will vanish the day macros are launched and an XML macro could be the next bet. Questions:
|
thanks for your review @bahag-chandrana , let me first shed some light on some key points before I answer your questions. The target of this generator is 100% spec compliance. Meaning that the generated spec files MUST be able to accurately reflect a given openapi specification, with all of its supported features. Of course the openapi spec is huge, and has some ambiguity. This is why I decided to first cover the most important aspect of openapi, schemas. A schema is essentially a description of a type. But openapi's only concern is validating a given json (or xml) value against a schema. it does NOT put any rules on what models should look like at the client side before they are sent to the server, and this is why I created this discussion before: OAI/OpenAPI-Specification#2807. This means that in order to achieve my goal, which is 100% compliance, I needed to introduce a lot of "new" concepts to dart to be able to perfectly represent a given schema. Problem 1: non-required nullable types Here is a fairly common schema: my_object:
type: object
properties:
name:
type: string
nullable: true the
{ "name": "a b c" }
{ "name": null }
{ } in a perfect world, if dart supported union types, we can represent this as follows: class Undefined {
const Undefined();
}
final <String | null | Undefined> name; and if name is However, the closest thing we have to union types is nullable types, e.g. So it's simply impossible to represent While i definitely agree that Also in your example
we can introduce an extension method to handle this common case extension UndefinedWrapperNullableExt<T> on UndefinedWrapper<T?> {
bool get isAvailable => src.split(
defined: (src) => src != null,
unDefined: () => false,
);
} also if dart-lang/language#3614 gets implemented, that cognitive load of having to wrap Problem 2: reflection As you know, using Now to implement an openapi schema as a dart class, the following features are expected of it:
And there is not a single existing generator based on So after getting fed up with all the existing This makes sure that tree shaking is applicable, and that we can follow separation of concerns principle, where the generated model classes only serve one purpose, storing values, and the reflection classes do the rest of the requested features. So for Question 1: All reflection classes extend The beautiful thing about this design, is that ALL the generated reflection classes can be replaced with macros! Question 2: Simply no, because there is no need to. Question 3: You can consider it an abstraction layer above This enables the user to define their own networking adapter that does NOT care about any of serialization logic, as it's only dealing with raw bytes and pure HTTP concepts. And this solves the main issue this generator was created for, decoupling networking from serialization |
Hello @ahmednfwela, thank you for the great contribution. I love the overall design, but.. |
maybe after I finish the initial PR and release it as experimental, I will add a flag to treat null as undefined if the property is not required |
Hi @ahmednfwela |
actually all the work is already done, the only thing missing is proper testing |
Pretty significant piece of work I would say. Could you clarify what is the roadmap before merging |
…to feat/dart-next
well, first I think i might need help with writing all the java tests, these are present in:
so that I can generate the samples and CI can pass. |
supersedes #15485
This generator is designed to replace both
dart
anddart-dio
generators, and has the following features:dart run build_runner build
).UndefinedWrapper<T>
which makes any non-required variable recognizeundefined
as a valid value, in which case, it will NOT be included during serialization.NetworkingClientBase
package:http
, butpackage:dio
can be added as wellshared_infrastructure
package that has simple base models that should NOT be changed, but the generator will also include an option to inline it. this is done so we can maintain a version of it on pub.dev which users can opt-in to use.package:cross_file
to handle filesCurrent tasks:
P.S. testing and templating for this generator is done here https://github.com/ahmednfwela/dart-next-generator-testing
PR checklist
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.
master
(upcoming 7.6.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)