diff --git a/lib/pipeline/generate/impl/model/model_content_builders.dart b/lib/pipeline/generate/impl/model/model_content_builders.dart index 22c8796..354a434 100644 --- a/lib/pipeline/generate/impl/model/model_content_builders.dart +++ b/lib/pipeline/generate/impl/model/model_content_builders.dart @@ -127,6 +127,8 @@ String _generateAnnotatedModelCodeWithoutImports( buffer.writeln(StringHelper.generateComment(model.description)); } + // Build @JsonSerializable annotation string for factory constructor + String? jsonSerializableAnnotation; final jsonConfig = SwaggerConfig.jsonSerializableConfig; if (jsonConfig != null) { final params = []; @@ -135,14 +137,19 @@ String _generateAnnotatedModelCodeWithoutImports( if (!jsonConfig.includeIfNull) params.add('includeIfNull: false'); if (params.isNotEmpty) { - buffer.writeln('@JsonSerializable(${params.join(', ')})'); + jsonSerializableAnnotation = '@JsonSerializable(${params.join(', ')})'; } } buffer ..writeln('@freezed') - ..writeln('abstract class $className with _\$$className {') - ..writeln(' const factory $className({'); + ..writeln('abstract class $className with _\$$className {'); + + // Add @JsonSerializable annotation on factory constructor if configured + if (jsonSerializableAnnotation != null) { + buffer.writeln(' $jsonSerializableAnnotation'); + } + buffer.writeln(' const factory $className({'); model.properties.forEach((propName, property) { final dartType = generator.getDartPropertyType(property);