Browse Source

dynamite: Implement required object properties

pull/93/head
jld3103 2 years ago
parent
commit
680501f319
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 3
      packages/dynamite/lib/src/models/schema.dart
  2. 3
      packages/dynamite/lib/src/models/schema.g.dart
  3. 15
      packages/dynamite/lib/src/openapi_builder.dart

3
packages/dynamite/lib/src/models/schema.dart

@ -16,6 +16,7 @@ class Schema {
required this.default_, required this.default_,
required this.enum_, required this.enum_,
required this.properties, required this.properties,
required this.required,
required this.items, required this.items,
}); });
@ -49,5 +50,7 @@ class Schema {
final Map<String, Schema>? properties; final Map<String, Schema>? properties;
final List<String>? required;
final Schema? items; final Schema? items;
} }

3
packages/dynamite/lib/src/models/schema.g.dart

@ -21,6 +21,7 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
'default', 'default',
'enum', 'enum',
'properties', 'properties',
'required',
'items' 'items'
], ],
); );
@ -38,6 +39,7 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
properties: (json['properties'] as Map<String, dynamic>?)?.map( properties: (json['properties'] as Map<String, dynamic>?)?.map(
(k, e) => MapEntry(k, Schema.fromJson(e as Map<String, dynamic>)), (k, e) => MapEntry(k, Schema.fromJson(e as Map<String, dynamic>)),
), ),
required: (json['required'] as List<dynamic>?)?.map((e) => e as String).toList(),
items: json['items'] == null ? null : Schema.fromJson(json['items'] as Map<String, dynamic>), items: json['items'] == null ? null : Schema.fromJson(json['items'] as Map<String, dynamic>),
); );
} }
@ -62,6 +64,7 @@ Map<String, dynamic> _$SchemaToJson(Schema instance) {
writeNotNull('default', instance.default_); writeNotNull('default', instance.default_);
writeNotNull('enum', instance.enum_); writeNotNull('enum', instance.enum_);
writeNotNull('properties', instance.properties?.map((k, e) => MapEntry(k, e.toJson()))); writeNotNull('properties', instance.properties?.map((k, e) => MapEntry(k, e.toJson())));
writeNotNull('required', instance.required);
writeNotNull('items', instance.items?.toJson()); writeNotNull('items', instance.items?.toJson());
return val; return val;
} }

15
packages/dynamite/lib/src/openapi_builder.dart

@ -462,7 +462,8 @@ class OpenAPIBuilder implements Builder {
(final b) => b (final b) => b
..name = _toDartName(propertyName) ..name = _toDartName(propertyName)
..toThis = true ..toThis = true
..named = true, ..named = true
..required = (schema.required ?? []).contains(propertyName),
), ),
), ),
), ),
@ -497,10 +498,7 @@ class OpenAPIBuilder implements Builder {
Field( Field(
(final b) { (final b) {
final result = resolveType( final result = resolveType(
_toDartName( '${identifier}_${_toDartName(propertyName, uppercaseFirstCharacter: true)}',
'$identifier${_toDartName(propertyName, uppercaseFirstCharacter: true)}',
uppercaseFirstCharacter: true,
),
schema.properties![propertyName]!, schema.properties![propertyName]!,
extraJsonSerializableValues: extraJsonSerializableValues, extraJsonSerializableValues: extraJsonSerializableValues,
); );
@ -509,7 +507,12 @@ class OpenAPIBuilder implements Builder {
b b
..name = _toDartName(propertyName) ..name = _toDartName(propertyName)
..type = refer(_makeNullable(result.typeName, true)) ..type = refer(
_makeNullable(
result.typeName,
!(schema.required ?? []).contains(propertyName),
),
)
..modifier = FieldModifier.final$ ..modifier = FieldModifier.final$
..docs.addAll([ ..docs.addAll([
if (propertySchema.description != null && if (propertySchema.description != null &&

Loading…
Cancel
Save