diff --git a/packages/dynamite/lib/src/models/schema.dart b/packages/dynamite/lib/src/models/schema.dart index b3caf1fd..0b3e48fd 100644 --- a/packages/dynamite/lib/src/models/schema.dart +++ b/packages/dynamite/lib/src/models/schema.dart @@ -16,6 +16,7 @@ class Schema { required this.default_, required this.enum_, required this.properties, + required this.required, required this.items, }); @@ -49,5 +50,7 @@ class Schema { final Map? properties; + final List? required; + final Schema? items; } diff --git a/packages/dynamite/lib/src/models/schema.g.dart b/packages/dynamite/lib/src/models/schema.g.dart index 9db4f07e..3f42b070 100644 --- a/packages/dynamite/lib/src/models/schema.g.dart +++ b/packages/dynamite/lib/src/models/schema.g.dart @@ -21,6 +21,7 @@ Schema _$SchemaFromJson(Map json) { 'default', 'enum', 'properties', + 'required', 'items' ], ); @@ -38,6 +39,7 @@ Schema _$SchemaFromJson(Map json) { properties: (json['properties'] as Map?)?.map( (k, e) => MapEntry(k, Schema.fromJson(e as Map)), ), + required: (json['required'] as List?)?.map((e) => e as String).toList(), items: json['items'] == null ? null : Schema.fromJson(json['items'] as Map), ); } @@ -62,6 +64,7 @@ Map _$SchemaToJson(Schema instance) { writeNotNull('default', instance.default_); writeNotNull('enum', instance.enum_); writeNotNull('properties', instance.properties?.map((k, e) => MapEntry(k, e.toJson()))); + writeNotNull('required', instance.required); writeNotNull('items', instance.items?.toJson()); return val; } diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index c90dd500..633589b1 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -462,7 +462,8 @@ class OpenAPIBuilder implements Builder { (final b) => b ..name = _toDartName(propertyName) ..toThis = true - ..named = true, + ..named = true + ..required = (schema.required ?? []).contains(propertyName), ), ), ), @@ -497,10 +498,7 @@ class OpenAPIBuilder implements Builder { Field( (final b) { final result = resolveType( - _toDartName( - '$identifier${_toDartName(propertyName, uppercaseFirstCharacter: true)}', - uppercaseFirstCharacter: true, - ), + '${identifier}_${_toDartName(propertyName, uppercaseFirstCharacter: true)}', schema.properties![propertyName]!, extraJsonSerializableValues: extraJsonSerializableValues, ); @@ -509,7 +507,12 @@ class OpenAPIBuilder implements Builder { b ..name = _toDartName(propertyName) - ..type = refer(_makeNullable(result.typeName, true)) + ..type = refer( + _makeNullable( + result.typeName, + !(schema.required ?? []).contains(propertyName), + ), + ) ..modifier = FieldModifier.final$ ..docs.addAll([ if (propertySchema.description != null &&