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.enum_,
required this.properties,
required this.required,
required this.items,
});
@ -49,5 +50,7 @@ class Schema {
final Map<String, Schema>? properties;
final List<String>? required;
final Schema? items;
}

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

@ -21,6 +21,7 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
'default',
'enum',
'properties',
'required',
'items'
],
);
@ -38,6 +39,7 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
properties: (json['properties'] as Map<String, dynamic>?)?.map(
(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>),
);
}
@ -62,6 +64,7 @@ Map<String, dynamic> _$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;
}

15
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 &&

Loading…
Cancel
Save