Browse Source

Merge pull request #223 from provokateurin/fix/dynamite-default-values

dynamite: Fix parameters with default values
pull/225/head
Kate 2 years ago committed by GitHub
parent
commit
b44aa7d7ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      packages/dynamite/lib/src/openapi_builder.dart

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

@ -614,7 +614,7 @@ class OpenAPIBuilder implements Builder {
spec, spec,
state, state,
_toDartName( _toDartName(
parameter.name, '$operationId-${parameter.name}',
uppercaseFirstCharacter: true, uppercaseFirstCharacter: true,
), ),
parameter.schema!, parameter.schema!,
@ -645,7 +645,7 @@ class OpenAPIBuilder implements Builder {
} }
final defaultValueCode = parameter.schema?.default_ != null final defaultValueCode = parameter.schema?.default_ != null
? _valueToEscapedValue(result.name, parameter.schema!.default_!.toString()) ? _valueToEscapedValue(result, parameter.schema!.default_!.toString())
: null; : null;
b.optionalParameters.add( b.optionalParameters.add(
@ -1081,7 +1081,18 @@ String _toFieldName(final String dartName, final String type) => dartName == typ
bool _isParameterNullable(final bool? required, final dynamic default_) => !(required ?? false) && default_ == null; bool _isParameterNullable(final bool? required, final dynamic default_) => !(required ?? false) && default_ == null;
String _valueToEscapedValue(final String type, final dynamic value) => type == 'String' ? "'$value'" : value.toString(); String _valueToEscapedValue(final TypeResult result, final dynamic value) {
if (result is TypeResultBase && result.name == 'String') {
return "'$value'";
}
if (result is TypeResultList) {
return 'const $value';
}
if (result is TypeResultEnum) {
return '${result.name}.${_toDartName(value.toString())}';
}
return value.toString();
}
List<String> _descriptionToDocs(final String? description) => [ List<String> _descriptionToDocs(final String? description) => [
if (description != null && description.isNotEmpty) ...[ if (description != null && description.isNotEmpty) ...[
@ -1168,7 +1179,7 @@ TypeResult resolveObject(
propertySchema.type!, propertySchema.type!,
propertySchema, propertySchema,
); );
b.defaultTo = Code(_valueToEscapedValue(result.name, value)); b.defaultTo = Code(_valueToEscapedValue(result, value));
} }
}, },
), ),
@ -1691,7 +1702,7 @@ TypeResult resolveType(
b b
..name = _toDartName(value.toString()) ..name = _toDartName(value.toString())
..arguments.add( ..arguments.add(
refer(_valueToEscapedValue(result.name, value)), refer(_valueToEscapedValue(result, value)),
); );
if (_toDartName(value.toString()) != value.toString()) { if (_toDartName(value.toString()) != value.toString()) {
if (result.name != 'String' && result.name != 'int') { if (result.name != 'String' && result.name != 'int') {
@ -1703,7 +1714,7 @@ TypeResult resolveType(
} }
b.annotations.add( b.annotations.add(
refer('JsonValue').call([ refer('JsonValue').call([
refer(_valueToEscapedValue(result.name, value.toString())), refer(_valueToEscapedValue(result, value.toString())),
]), ]),
); );
} }
@ -1728,7 +1739,7 @@ TypeResult resolveType(
[ [
'switch (value) {', 'switch (value) {',
for (final value in schema.enum_!) ...[ for (final value in schema.enum_!) ...[
'case ${_valueToEscapedValue(result!.name, value)}:', 'case ${_valueToEscapedValue(result!, value)}:',
'return ${state.prefix}$identifier.${_toDartName(value.toString())};', 'return ${state.prefix}$identifier.${_toDartName(value.toString())};',
], ],
'default:', 'default:',

Loading…
Cancel
Save