Browse Source

feat(dynamite): Support content in parameters

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/1020/head
jld3103 1 year ago
parent
commit
1429cdece5
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 2
      packages/dynamite/dynamite/lib/src/models/openapi.g.dart
  2. 35
      packages/dynamite/dynamite/lib/src/models/openapi/parameter.dart
  3. 53
      packages/dynamite/dynamite/lib/src/models/openapi/parameter.g.dart

2
packages/dynamite/dynamite/lib/src/models/openapi.g.dart

@ -63,6 +63,8 @@ Serializers _$serializers = (Serializers().toBuilder()
const FullType(BuiltMap, [FullType(String), FullType(MediaType)]), () => MapBuilder<String, MediaType>()) const FullType(BuiltMap, [FullType(String), FullType(MediaType)]), () => MapBuilder<String, MediaType>())
..addBuilderFactory( ..addBuilderFactory(
const FullType(BuiltMap, [FullType(String), FullType(MediaType)]), () => MapBuilder<String, MediaType>()) const FullType(BuiltMap, [FullType(String), FullType(MediaType)]), () => MapBuilder<String, MediaType>())
..addBuilderFactory(
const FullType(BuiltMap, [FullType(String), FullType(MediaType)]), () => MapBuilder<String, MediaType>())
..addBuilderFactory( ..addBuilderFactory(
const FullType(BuiltMap, [FullType(String), FullType(Header)]), () => MapBuilder<String, Header>()) const FullType(BuiltMap, [FullType(String), FullType(Header)]), () => MapBuilder<String, Header>())
..addBuilderFactory(const FullType(BuiltMap, [FullType(String), FullType(SecurityScheme)]), ..addBuilderFactory(const FullType(BuiltMap, [FullType(String), FullType(SecurityScheme)]),

35
packages/dynamite/dynamite/lib/src/models/openapi/parameter.dart

@ -4,6 +4,7 @@ import 'package:built_value/serializer.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart'; import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:dynamite/src/helpers/docs.dart'; import 'package:dynamite/src/helpers/docs.dart';
import 'package:dynamite/src/models/exceptions.dart'; import 'package:dynamite/src/models/exceptions.dart';
import 'package:dynamite/src/models/openapi/media_type.dart';
import 'package:dynamite/src/models/openapi/schema.dart'; import 'package:dynamite/src/models/openapi/schema.dart';
part 'parameter.g.dart'; part 'parameter.g.dart';
@ -25,7 +26,33 @@ abstract class Parameter implements Built<Parameter, ParameterBuilder> {
bool get required; bool get required;
Schema? get schema; @Deprecated('Use [schema] instead which also automatically handles [content].')
@BuiltValueField(wireName: 'schema')
Schema? get $schema;
BuiltMap<String, MediaType>? get content;
Schema? get schema {
// ignore: deprecated_member_use_from_same_package
if ($schema != null) {
// ignore: deprecated_member_use_from_same_package
return $schema;
}
if (content != null && content!.isNotEmpty) {
if (content!.length > 1) {
print('Can not work with multiple mime types right now. Using the first supported.');
}
return Schema(
(final b) => b
..type = SchemaType.string
..contentMediaType = content!.entries.first.key
..contentSchema = content!.entries.first.value.schema!.toBuilder(),
);
}
return null;
}
@BuiltValueHook(finalizeBuilder: true) @BuiltValueHook(finalizeBuilder: true)
static void _defaults(final ParameterBuilder b) { static void _defaults(final ParameterBuilder b) {
@ -34,9 +61,13 @@ abstract class Parameter implements Built<Parameter, ParameterBuilder> {
throw OpenAPISpecError('Path parameters must be required but ${b.name} is not.'); throw OpenAPISpecError('Path parameters must be required but ${b.name} is not.');
} }
if (b.required! && b.schema.$default != null) { if (b.required! && b._$schema != null && b.$schema.$default != null) {
print('Required parameters should not specify default values.'); print('Required parameters should not specify default values.');
} }
if (b._$schema != null && b._content != null) {
throw OpenAPISpecError('Only one of schema or content must be set in parameter ${b.name}.');
}
} }
String get formattedDescription { String get formattedDescription {

53
packages/dynamite/dynamite/lib/src/models/openapi/parameter.g.dart

@ -60,12 +60,19 @@ class _$ParameterSerializer implements StructuredSerializer<Parameter> {
..add('description') ..add('description')
..add(serializers.serialize(value, specifiedType: const FullType(String))); ..add(serializers.serialize(value, specifiedType: const FullType(String)));
} }
value = object.schema; value = object.$schema;
if (value != null) { if (value != null) {
result result
..add('schema') ..add('schema')
..add(serializers.serialize(value, specifiedType: const FullType(Schema))); ..add(serializers.serialize(value, specifiedType: const FullType(Schema)));
} }
value = object.content;
if (value != null) {
result
..add('content')
..add(serializers.serialize(value,
specifiedType: const FullType(BuiltMap, [FullType(String), FullType(MediaType)])));
}
return result; return result;
} }
@ -93,7 +100,11 @@ class _$ParameterSerializer implements StructuredSerializer<Parameter> {
result.required = serializers.deserialize(value, specifiedType: const FullType(bool))! as bool; result.required = serializers.deserialize(value, specifiedType: const FullType(bool))! as bool;
break; break;
case 'schema': case 'schema':
result.schema.replace(serializers.deserialize(value, specifiedType: const FullType(Schema))! as Schema); result.$schema.replace(serializers.deserialize(value, specifiedType: const FullType(Schema))! as Schema);
break;
case 'content':
result.content.replace(serializers.deserialize(value,
specifiedType: const FullType(BuiltMap, [FullType(String), FullType(MediaType)]))!);
break; break;
} }
} }
@ -128,11 +139,14 @@ class _$Parameter extends Parameter {
@override @override
final bool required; final bool required;
@override @override
final Schema? schema; final Schema? $schema;
@override
final BuiltMap<String, MediaType>? content;
factory _$Parameter([void Function(ParameterBuilder)? updates]) => (ParameterBuilder()..update(updates))._build(); factory _$Parameter([void Function(ParameterBuilder)? updates]) => (ParameterBuilder()..update(updates))._build();
_$Parameter._({required this.name, required this.$in, this.description, required this.required, this.schema}) _$Parameter._(
{required this.name, required this.$in, this.description, required this.required, this.$schema, this.content})
: super._() { : super._() {
BuiltValueNullFieldError.checkNotNull(name, r'Parameter', 'name'); BuiltValueNullFieldError.checkNotNull(name, r'Parameter', 'name');
BuiltValueNullFieldError.checkNotNull($in, r'Parameter', '\$in'); BuiltValueNullFieldError.checkNotNull($in, r'Parameter', '\$in');
@ -152,7 +166,8 @@ class _$Parameter extends Parameter {
name == other.name && name == other.name &&
$in == other.$in && $in == other.$in &&
required == other.required && required == other.required &&
schema == other.schema; $schema == other.$schema &&
content == other.content;
} }
@override @override
@ -161,7 +176,8 @@ class _$Parameter extends Parameter {
_$hash = $jc(_$hash, name.hashCode); _$hash = $jc(_$hash, name.hashCode);
_$hash = $jc(_$hash, $in.hashCode); _$hash = $jc(_$hash, $in.hashCode);
_$hash = $jc(_$hash, required.hashCode); _$hash = $jc(_$hash, required.hashCode);
_$hash = $jc(_$hash, schema.hashCode); _$hash = $jc(_$hash, $schema.hashCode);
_$hash = $jc(_$hash, content.hashCode);
_$hash = $jf(_$hash); _$hash = $jf(_$hash);
return _$hash; return _$hash;
} }
@ -173,7 +189,8 @@ class _$Parameter extends Parameter {
..add('\$in', $in) ..add('\$in', $in)
..add('description', description) ..add('description', description)
..add('required', required) ..add('required', required)
..add('schema', schema)) ..add('\$schema', $schema)
..add('content', content))
.toString(); .toString();
} }
} }
@ -197,9 +214,13 @@ class ParameterBuilder implements Builder<Parameter, ParameterBuilder> {
bool? get required => _$this._required; bool? get required => _$this._required;
set required(bool? required) => _$this._required = required; set required(bool? required) => _$this._required = required;
SchemaBuilder? _schema; SchemaBuilder? _$schema;
SchemaBuilder get schema => _$this._schema ??= SchemaBuilder(); SchemaBuilder get $schema => _$this._$schema ??= SchemaBuilder();
set schema(SchemaBuilder? schema) => _$this._schema = schema; set $schema(SchemaBuilder? $schema) => _$this._$schema = $schema;
MapBuilder<String, MediaType>? _content;
MapBuilder<String, MediaType> get content => _$this._content ??= MapBuilder<String, MediaType>();
set content(MapBuilder<String, MediaType>? content) => _$this._content = content;
ParameterBuilder(); ParameterBuilder();
@ -210,7 +231,8 @@ class ParameterBuilder implements Builder<Parameter, ParameterBuilder> {
_$in = $v.$in; _$in = $v.$in;
_description = $v.description; _description = $v.description;
_required = $v.required; _required = $v.required;
_schema = $v.schema?.toBuilder(); _$schema = $v.$schema?.toBuilder();
_content = $v.content?.toBuilder();
_$v = null; _$v = null;
} }
return this; return this;
@ -240,12 +262,15 @@ class ParameterBuilder implements Builder<Parameter, ParameterBuilder> {
$in: BuiltValueNullFieldError.checkNotNull($in, r'Parameter', '\$in'), $in: BuiltValueNullFieldError.checkNotNull($in, r'Parameter', '\$in'),
description: description, description: description,
required: BuiltValueNullFieldError.checkNotNull(required, r'Parameter', 'required'), required: BuiltValueNullFieldError.checkNotNull(required, r'Parameter', 'required'),
schema: _schema?.build()); $schema: _$schema?.build(),
content: _content?.build());
} catch (_) { } catch (_) {
late String _$failedField; late String _$failedField;
try { try {
_$failedField = 'schema'; _$failedField = '\$schema';
_schema?.build(); _$schema?.build();
_$failedField = 'content';
_content?.build();
} catch (e) { } catch (e) {
throw BuiltValueNestedFieldError(r'Parameter', _$failedField, e.toString()); throw BuiltValueNestedFieldError(r'Parameter', _$failedField, e.toString());
} }

Loading…
Cancel
Save