Browse Source

dynamite: Support strings with patterns

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

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

@ -23,6 +23,7 @@ class Schema {
this.contentMediaType, this.contentMediaType,
this.contentSchema, this.contentSchema,
this.discriminator, this.discriminator,
this.pattern,
}); });
factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json); factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json);
@ -67,5 +68,7 @@ class Schema {
final Discriminator? discriminator; final Discriminator? discriminator;
final String? pattern;
bool get isContentString => type == 'string' && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null; bool get isContentString => type == 'string' && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null;
} }

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

@ -26,7 +26,8 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
'additionalProperties', 'additionalProperties',
'contentMediaType', 'contentMediaType',
'contentSchema', 'contentSchema',
'discriminator' 'discriminator',
'pattern'
], ],
); );
return Schema( return Schema(
@ -51,6 +52,7 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
json['contentSchema'] == null ? null : Schema.fromJson(json['contentSchema'] as Map<String, dynamic>), json['contentSchema'] == null ? null : Schema.fromJson(json['contentSchema'] as Map<String, dynamic>),
discriminator: discriminator:
json['discriminator'] == null ? null : Discriminator.fromJson(json['discriminator'] as Map<String, dynamic>), json['discriminator'] == null ? null : Discriminator.fromJson(json['discriminator'] as Map<String, dynamic>),
pattern: json['pattern'] as String?,
); );
} }
@ -80,5 +82,6 @@ Map<String, dynamic> _$SchemaToJson(Schema instance) {
writeNotNull('contentMediaType', instance.contentMediaType); writeNotNull('contentMediaType', instance.contentMediaType);
writeNotNull('contentSchema', instance.contentSchema?.toJson()); writeNotNull('contentSchema', instance.contentSchema?.toJson());
writeNotNull('discriminator', instance.discriminator?.toJson()); writeNotNull('discriminator', instance.discriminator?.toJson());
writeNotNull('pattern', instance.pattern);
return val; return val;
} }

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

@ -574,6 +574,14 @@ class OpenAPIBuilder implements Builder {
parameter.schema!, parameter.schema!,
); );
if (result.name == 'String' && parameter.schema?.pattern != null) {
code.write('''
if (!RegExp(r'${parameter.schema!.pattern!}').hasMatch(${_toDartName(parameter.name)})) {
throw Exception('Invalid value "\$${_toDartName(parameter.name)}" for parameter "${_toDartName(parameter.name)}" with pattern "' r'${parameter.schema!.pattern!}"'); // coverage:ignore-line
}
''');
}
final defaultValueCode = parameter.schema?.default_ != null final defaultValueCode = parameter.schema?.default_ != null
? _valueToEscapedValue(result.name, parameter.schema!.default_!.toString()) ? _valueToEscapedValue(result.name, parameter.schema!.default_!.toString())
: null; : null;

Loading…
Cancel
Save