[dart-dio] Fix inheritance with json_serializable using super parameters#23329
Open
AshitaNema wants to merge 4 commits intoOpenAPITools:masterfrom
Open
[dart-dio] Fix inheritance with json_serializable using super parameters#23329AshitaNema wants to merge 4 commits intoOpenAPITools:masterfrom
AshitaNema wants to merge 4 commits intoOpenAPITools:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
2 issues found across 55 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/dog.dart">
<violation number="1" location="samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/dog.dart:22">
P2: Inheritance introduced asymmetric `==` semantics between `Animal` and `Dog`, violating equality contract.</violation>
</file>
<file name="modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/dart_constructor.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/dart_constructor.mustache:11">
P1: Inherited constructor params (`parentVars`) omit `useOptional` handling, causing inconsistent and potentially incorrect generation for optional-wrapper inherited fields.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
.../main/resources/dart/libraries/dio/serialization/json_serializable/dart_constructor.mustache
Outdated
Show resolved
Hide resolved
...3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/dog.dart
Show resolved
Hide resolved
…ers - Add 'extends' clause to child classes when parent exists - Use Dart 2.17+ super parameters (super.property) in constructors - Separate child properties (this.name) from parent properties (super.name) - Set parentVars in postProcessAllModels for json_serializable - Only declare child properties in class body (not parent properties) - Use allVars for equals/hashCode to include all properties Fixes OpenAPITools#22547
Member
|
cc @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08) |
Contributor
There was a problem hiding this comment.
2 issues found across 49 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/animal.dart">
<violation number="1">
P2: Removing the runtimeType check in Animal allows Animal == Dog when base fields match, while Dog == Animal remains false (Dog uses `other is Dog` and includes `breed`). This breaks equality/hashCode symmetry across the inheritance hierarchy and can cause incorrect Set/Map behavior.</violation>
</file>
<file name="samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/parent_with_nullable.dart">
<violation number="1">
P2: Removing the runtimeType guard allows ParentWithNullable to consider subclass instances equal, which is asymmetric with ChildWithNullable’s overridden == and can break hash/equality behavior.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
...lient/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/animal.dart
Show resolved
Hide resolved
.../dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/parent_with_nullable.dart
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix for Dart-Dio Generator: Super Constructor Parameters for Inheritance
Summary
Fixed issue #22547 where the dart-dio generator with json_serializable was not properly handling inheritance. Child classes were not calling the parent constructor with required parameters, causing compilation errors in generated Dart code.
Problem
When using
allOfwith the dart-dio generator and json_serializable library, child models that inherited from parent models would fail to compile because:super()with parent propertiesparentVarsfield was not being populatedExample Issue
For this schema:
Before (broken):
After (fixed):
Solution
1. Java Code Changes
DartDioClientCodegen.java
Constructor: Added
this.supportsInheritance = trueto prevent DefaultCodegen from copying parent properties into child's 'vars'postProcessAllModels: Added inheritance processing for json_serializable only:
parentVarsfrom the parent model'svarsvarsto avoid duplicationserializationLibrary == json_serializable(built_value has its own adaptToDartInheritance logic)2. Template Changes
class.mustache
extends {{{parent}}}to class declaration when parent exists{{#allVars}}to{{#vars}}so only child properties are declared{{#allVars}}for equals/hashCode to include both parent and child properties in equality checksdart_constructor.mustache
{{#vars}}) from parent properties (using{{#parentVars}})this.{name}syntax (they're class fields)super.{name}syntax (Dart 2.17+ super parameters): super(...)initializer list and parameter redeclarationKey Variables Used
After the fix, for Dog model:
[breed]- only child's own properties[className, color]- parent's properties[className, color, breed]- all properties combined (for equals/hashCode)Files Modified
Core Changes:
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.javamodules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/dart_constructor.mustachemodules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustacheSample Files (regenerated):
samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/Testing
Verified the fix works correctly with the Animal/Dog/Cat inheritance example from the test suite.
To regenerate samples:
Notes
serializationLibrary: json_serializablebuilt_valueserialization already has proper inheritance handling viaadaptToDartInheritance()References
Summary by cubic
Fixes broken inheritance in
dart-diowithjson_serializableby making child models extend parents and pass required fields via Dart super parameters. Removes duplicated fields, tightens equality, and stops compile errors.supportsInheritance = true) to stop copying parent props into childvars.json_serializableonly, setparentVarsfrom the parent and remove duplicates from childvarsinpostProcessAllModels.extends {{parent}}; usesuper.{name}for parent constructor params; declare only child fields; useallVarsplus aruntimeTypecheck in equality/hash.petstore_client_lib_fake-json_serializablecode and docs; child model docs list only child properties; no change tobuilt_value.Written for commit e2539dc. Summary will update on new commits.