From 369071902f1ae464c6dd04a1640d4dd6d26c6159 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 14 Mar 2023 11:03:06 +0100 Subject: [PATCH] dynamite: Fix array parameters with default values --- .../dynamite/lib/src/openapi_builder.dart | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 8a89894d..fe47e1b0 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -635,7 +635,7 @@ class OpenAPIBuilder implements Builder { } final defaultValueCode = parameter.schema?.default_ != null - ? _valueToEscapedValue(result.name, parameter.schema!.default_!.toString()) + ? _valueToEscapedValue(result, parameter.schema!.default_!.toString()) : null; b.optionalParameters.add( @@ -1071,7 +1071,15 @@ String _toFieldName(final String dartName, final String type) => dartName == typ 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'; + } + return value.toString(); +} List _descriptionToDocs(final String? description) => [ if (description != null && description.isNotEmpty) ...[ @@ -1158,7 +1166,7 @@ TypeResult resolveObject( propertySchema.type!, propertySchema, ); - b.defaultTo = Code(_valueToEscapedValue(result.name, value)); + b.defaultTo = Code(_valueToEscapedValue(result, value)); } }, ), @@ -1681,7 +1689,7 @@ TypeResult resolveType( b ..name = _toDartName(value.toString()) ..arguments.add( - refer(_valueToEscapedValue(result.name, value)), + refer(_valueToEscapedValue(result, value)), ); if (_toDartName(value.toString()) != value.toString()) { if (result.name != 'String' && result.name != 'int') { @@ -1693,7 +1701,7 @@ TypeResult resolveType( } b.annotations.add( refer('JsonValue').call([ - refer(_valueToEscapedValue(result.name, value.toString())), + refer(_valueToEscapedValue(result, value.toString())), ]), ); } @@ -1718,7 +1726,7 @@ TypeResult resolveType( [ 'switch (value) {', for (final value in schema.enum_!) ...[ - 'case ${_valueToEscapedValue(result!.name, value)}:', + 'case ${_valueToEscapedValue(result!, value)}:', 'return ${state.prefix}$identifier.${_toDartName(value.toString())};', ], 'default:',