Skip to content

Commit 2b63fc5

Browse files
author
Ashita Nema
committed
Fix for Dart-Dio Generator: Super Constructor Parameters for Inheritance
1 parent 88f05a9 commit 2b63fc5

55 files changed

Lines changed: 125 additions & 292 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public class DartDioClientCodegen extends AbstractDartCodegen {
8787

8888
public DartDioClientCodegen() {
8989
super();
90+
91+
// This stops the DefaultCodegen from copying parent properties into the child's 'vars'.
92+
this.supportsInheritance = true;
9093

9194
modifyFeatureSet(features -> features
9295
.includeClientModificationFeatures(
@@ -606,6 +609,52 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
606609
@Override
607610
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
608611
objs = super.postProcessAllModels(objs);
612+
613+
// For json_serializable, properly set up parent variables for inheritance
614+
if (SERIALIZATION_LIBRARY_JSON_SERIALIZABLE.equals(library)) {
615+
// Build a map of all models for quick lookup
616+
Map<String, CodegenModel> allModelsMap = new HashMap<>();
617+
for (ModelsMap modelsEntries : objs.values()) {
618+
for (ModelMap modelsMap : modelsEntries.getModels()) {
619+
allModelsMap.put(modelsMap.getModel().getClassname(), modelsMap.getModel());
620+
}
621+
}
622+
623+
// Process models with inheritance
624+
for (ModelsMap modelsEntries : objs.values()) {
625+
for (ModelMap mo : modelsEntries.getModels()) {
626+
CodegenModel cm = mo.getModel();
627+
628+
if (cm.getParent() != null && !cm.getParent().isEmpty()) {
629+
LOGGER.debug("Processing inheritance for model: {} (extends: {})", cm.getClassname(), cm.getParent());
630+
631+
CodegenModel parentModel = allModelsMap.get(cm.getParent());
632+
633+
if (parentModel != null && parentModel.getVars() != null && !parentModel.getVars().isEmpty()) {
634+
// Set parent variables
635+
cm.setParentVars(parentModel.getVars());
636+
637+
// Remove parent properties from child's vars to avoid duplication
638+
Set<String> parentVarNames = cm.getParentVars().stream()
639+
.map(CodegenProperty::getName)
640+
.collect(Collectors.toSet());
641+
642+
cm.setVars(cm.getVars().stream()
643+
.filter(p -> !parentVarNames.contains(p.getName()))
644+
.collect(Collectors.toList())
645+
);
646+
647+
LOGGER.debug("Inheritance setup complete for {}: {} parent properties moved to parentVars",
648+
cm.getClassname(), parentModel.getVars().size());
649+
} else {
650+
LOGGER.warn("Parent model '{}' not found or has no properties for child model '{}'",
651+
cm.getParent(), cm.getClassname());
652+
}
653+
}
654+
}
655+
}
656+
}
657+
609658
if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library)) {
610659
adaptToDartInheritance(objs);
611660
syncRootTypesWithInnerVars(objs);

modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ part '{{classFilename}}.g.dart';
3232
disallowUnrecognizedKeys: false,
3333
explicitToJson: true,
3434
)
35-
class {{{classname}}} {
35+
class {{{classname}}}{{#parent}} extends {{{parent}}}{{/parent}} {
3636
{{>serialization/json_serializable/dart_constructor}}
3737

3838

@@ -89,14 +89,14 @@ class {{{classname}}} {
8989
runtimeType == other.runtimeType &&
9090
equals(
9191
[
92-
{{#vars}}
92+
{{#allVars}}
9393
{{{name}}},
94-
{{/vars}}
94+
{{/allVars}}
9595
],
9696
[
97-
{{#vars}}
97+
{{#allVars}}
9898
other.{{{name}}},
99-
{{/vars}}
99+
{{/allVars}}
100100
]
101101
);
102102
}
@@ -105,25 +105,25 @@ class {{{classname}}} {
105105
{{^useEquatable}}
106106
@override
107107
bool operator ==(Object other) => identical(this, other) || other is {{{classname}}} &&
108-
{{#vars}}
108+
{{#allVars}}
109109
other.{{{name}}} == {{{name}}}{{^-last}} &&{{/-last}}{{#-last}};{{/-last}}
110-
{{/vars}}
110+
{{/allVars}}
111111
{{/useEquatable}}
112112

113113
{{#useEquatable}}
114114
@override
115115
int get hashCode => runtimeType.hashCode ^ mapPropsToHashCode([
116-
{{#vars}}
116+
{{#allVars}}
117117
{{{name}}},
118-
{{/vars}}
118+
{{/allVars}}
119119
],);
120120
{{/useEquatable}}
121121
{{^useEquatable}}
122122
@override
123123
int get hashCode =>
124-
{{#vars}}
124+
{{#allVars}}
125125
{{#isNullable}}({{{name}}} == null ? 0 : {{{name}}}.hashCode){{/isNullable}}{{^isNullable}}{{{name}}}.hashCode{{/isNullable}}{{^-last}} +{{/-last}}{{#-last}};{{/-last}}
126-
{{/vars}}
126+
{{/allVars}}
127127
{{/useEquatable}}
128128

129129
factory {{{classname}}}.fromJson(Map<String, dynamic> json) => _${{{classname}}}FromJson(json);
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
/// Returns a new [{{{classname}}}] instance.
1+
/// Returns a new [{{{classname}}}] instance.
22
{{{classname}}}({
3-
{{#vars}}
4-
3+
{{#vars}}
54
{{!
65
A field is required in Dart when it is
76
required && !defaultValue in OAS
87
}}
98
{{^required}}{{#useOptional}}this.{{{name}}}{{#defaultValue}} = const Optional.present({{#isEnum}}{{^isContainer}}const {{{enumName}}}._({{/isContainer}}{{/isEnum}}{{{defaultValue}}}{{#isEnum}}{{^isContainer}}){{/isContainer}}{{/isEnum}}){{/defaultValue}}{{^defaultValue}} = const Optional.absent(){{/defaultValue}},{{/useOptional}}{{/required}}{{^required}}{{^useOptional}} this.{{{name}}}{{#defaultValue}} = {{#isEnum}}{{^isContainer}}const {{{enumName}}}._({{/isContainer}}{{/isEnum}}{{{defaultValue}}}{{#isEnum}}{{^isContainer}}){{/isContainer}}{{/isEnum}}{{/defaultValue}},{{/useOptional}}{{/required}}{{#required}}{{^defaultValue}}required {{/defaultValue}}{{/required}}{{#required}} this.{{{name}}}{{#defaultValue}} = {{#isEnum}}{{^isContainer}}const {{{enumName}}}._({{/isContainer}}{{/isEnum}}{{{defaultValue}}}{{#isEnum}}{{^isContainer}}){{/isContainer}}{{/isEnum}}{{/defaultValue}},{{/required}}
109
{{/vars}}
10+
{{#parentVars}}
11+
{{#required}}{{^defaultValue}}required {{/defaultValue}}{{/required}} super.{{{name}}}{{#defaultValue}} = {{#isEnum}}{{^isContainer}}const {{{enumName}}}._({{/isContainer}}{{/isEnum}}{{{defaultValue}}}{{#isEnum}}{{^isContainer}}){{/isContainer}}{{/isEnum}}{{/defaultValue}},
12+
{{/parentVars}}
1113
});

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/Cat.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'package:openapi/api.dart';
88
## Properties
99
Name | Type | Description | Notes
1010
------------ | ------------- | ------------- | -------------
11-
**className** | **String** | |
12-
**color** | **String** | | [optional] [default to 'red']
1311
**declawed** | **bool** | | [optional]
1412

1513
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/ChildWithNullable.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'package:openapi/api.dart';
88
## Properties
99
Name | Type | Description | Notes
1010
------------ | ------------- | ------------- | -------------
11-
**type** | **String** | | [optional]
12-
**nullableProperty** | **String** | | [optional]
1311
**otherProperty** | **String** | | [optional]
1412

1513
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/Dog.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'package:openapi/api.dart';
88
## Properties
99
Name | Type | Description | Notes
1010
------------ | ------------- | ------------- | -------------
11-
**className** | **String** | |
12-
**color** | **String** | | [optional] [default to 'red']
1311
**breed** | **String** | | [optional]
1412

1513
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/additional_properties_class.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ part 'additional_properties_class.g.dart';
1717
explicitToJson: true,
1818
)
1919
class AdditionalPropertiesClass {
20-
/// Returns a new [AdditionalPropertiesClass] instance.
20+
/// Returns a new [AdditionalPropertiesClass] instance.
2121
AdditionalPropertiesClass({
22-
2322
this.mapProperty,
24-
2523
this.mapOfMapProperty,
2624

2725
this.mapOfArrayInteger,

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/all_of_with_single_ref.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ part 'all_of_with_single_ref.g.dart';
1818
explicitToJson: true,
1919
)
2020
class AllOfWithSingleRef {
21-
/// Returns a new [AllOfWithSingleRef] instance.
21+
/// Returns a new [AllOfWithSingleRef] instance.
2222
AllOfWithSingleRef({
23-
2423
this.username,
25-
2624
this.singleRefType,
2725
});
2826

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/animal.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ part 'animal.g.dart';
1717
explicitToJson: true,
1818
)
1919
class Animal {
20-
/// Returns a new [Animal] instance.
20+
/// Returns a new [Animal] instance.
2121
Animal({
22-
2322
required this.className,
24-
2523
this.color = 'red',
2624
});
2725

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/api_response.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ part 'api_response.g.dart';
1717
explicitToJson: true,
1818
)
1919
class ApiResponse {
20-
/// Returns a new [ApiResponse] instance.
20+
/// Returns a new [ApiResponse] instance.
2121
ApiResponse({
22-
2322
this.code,
24-
2523
this.type,
26-
2724
this.message,
2825
});
2926

0 commit comments

Comments
 (0)