From b61b61320e5d0939532a7df0e31bed0ed2403d52 Mon Sep 17 00:00:00 2001 From: Uladzimir Paliukhovich Date: Wed, 24 Jan 2024 17:30:00 +0300 Subject: [PATCH] Fixed generation of allOf models --- .../swagger_models_generator.dart | 29 +++++++++++++++++++ .../responses/swagger_schema.g.dart | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart index 25be4f2b..2e963a9b 100644 --- a/lib/src/code_generators/swagger_models_generator.dart +++ b/lib/src/code_generators/swagger_models_generator.dart @@ -145,6 +145,31 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { ); } + Map getAllOfProperties( + SwaggerSchema schema, + Map classes, + ) { + final properties = schema.properties; + for (var element in schema.allOf) { + properties.addAll(element.properties); + + if (element.ref.isNotEmpty) { + final neededClass = classes[element.ref.getUnformattedRef()]; + properties.addAll(neededClass?.properties ?? {}); + + if (neededClass != null) { + properties.addAll(getAllOfProperties(neededClass, classes)); + } + } else { + for (var allOf in element.allOf) { + properties.addAll(getAllOfProperties(allOf, classes)); + } + } + } + + return properties; + } + Map getClassesFromInnerClasses( Map classes, ) { @@ -156,6 +181,10 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase { ...schema.items?.properties ?? {}, }; + final allOfProperties = getAllOfProperties(schema, classes); + + properties.addAll(allOfProperties); + for (var element in schema.allOf) { properties.addAll(element.properties); diff --git a/lib/src/swagger_models/responses/swagger_schema.g.dart b/lib/src/swagger_models/responses/swagger_schema.g.dart index 9ff3f5ea..25dfb5b1 100644 --- a/lib/src/swagger_models/responses/swagger_schema.g.dart +++ b/lib/src/swagger_models/responses/swagger_schema.g.dart @@ -15,7 +15,7 @@ SwaggerSchema _$SwaggerSchemaFromJson(Map json) => (k, e) => MapEntry(k, SwaggerSchema.fromJson(e as Map)), ) ?? - const {}, + {}, items: json['items'] == null ? null : SwaggerSchema.fromJson(json['items'] as Map),