From 9fbff5a8dd4ef4abc70f543685a89cf1e162c335 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 18 Dec 2023 09:18:08 +0100 Subject: [PATCH] fix(dynamite): Fix pattern check for enums Signed-off-by: jld3103 --- .../dynamite/lib/src/builder/client.dart | 17 +- .../lib/src/builder/resolve_object.dart | 2 +- .../lib/src/helpers/pattern_check.dart | 9 +- .../lib/parameters.openapi.dart | 32 ++- .../lib/parameters.openapi.g.dart | 53 +++++ .../lib/parameters.openapi.json | 12 ++ .../lib/pattern_check.openapi.dart | 12 +- .../nextcloud/lib/src/api/files.openapi.dart | 2 +- .../lib/src/api/files_reminders.openapi.dart | 6 +- .../lib/src/api/provisioning_api.openapi.dart | 18 +- .../nextcloud/lib/src/api/spreed.openapi.dart | 188 +++++++++--------- .../lib/src/api/theming.openapi.dart | 4 +- 12 files changed, 221 insertions(+), 134 deletions(-) create mode 100644 packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.g.dart diff --git a/packages/dynamite/dynamite/lib/src/builder/client.dart b/packages/dynamite/dynamite/lib/src/builder/client.dart index 00dc72cb8bc..161d927948f 100644 --- a/packages/dynamite/dynamite/lib/src/builder/client.dart +++ b/packages/dynamite/dynamite/lib/src/builder/client.dart @@ -262,7 +262,6 @@ Iterable buildTags( ), ); - buildParameterPatternCheck(parameter).forEach(code.writeln); code.writeln(buildParameterSerialization(result, parameter)); } resolveMimeTypeEncode(operation, spec, state, operationName, operationParameters).forEach(code.writeln); @@ -444,6 +443,10 @@ String buildParameterSerialization( buffer.writeln('$serializedName ??= $defaultValueCode;'); } + if (parameter.schema != null) { + buildPatternCheck(parameter.schema!, serializedName, dartName).forEach(buffer.writeln); + } + if (parameter.$in == openapi.ParameterType.header) { final assignment = "_headers['${parameter.pctEncodedName}'] = ${result.encode(serializedName, onlyChildren: true)};"; @@ -463,18 +466,6 @@ String buildParameterSerialization( return buffer.toString(); } -Iterable buildParameterPatternCheck( - final openapi.Parameter parameter, -) sync* { - final schema = parameter.schema; - if (schema == null) { - return; - } - - final value = toDartName(parameter.name); - yield* buildPatternCheck(schema, value); -} - Iterable buildAuthCheck( final MapEntry pathEntry, final openapi.Operation operation, diff --git a/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart b/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart index acba3bcf8be..adbc7fb81a7 100644 --- a/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart +++ b/packages/dynamite/dynamite/lib/src/builder/resolve_object.dart @@ -79,7 +79,7 @@ TypeResultObject resolveObject( validators.add('b.$dartName?.validateAnyOf();'); } - validators.addAll(buildPatternCheck(propertySchema, 'b.$dartName')); + validators.addAll(buildPatternCheck(propertySchema, 'b.$dartName', dartName)); } final $interface = buildInterface( diff --git a/packages/dynamite/dynamite/lib/src/helpers/pattern_check.dart b/packages/dynamite/dynamite/lib/src/helpers/pattern_check.dart index a6239c09f38..3b75107f9bb 100644 --- a/packages/dynamite/dynamite/lib/src/helpers/pattern_check.dart +++ b/packages/dynamite/dynamite/lib/src/helpers/pattern_check.dart @@ -4,18 +4,17 @@ import 'package:dynamite/src/models/openapi/schema.dart'; Iterable buildPatternCheck( final openapi.Schema schema, final String value, + final String name, ) sync* { - final name = "'$value'"; - if (schema.type == SchemaType.string) { if (schema.pattern != null) { - yield "dynamite_utils.checkPattern($value, RegExp(r'${schema.pattern!}'), $name);"; + yield "dynamite_utils.checkPattern($value as String?, RegExp(r'${schema.pattern!}'), '$name');"; } if (schema.minLength != null) { - yield 'dynamite_utils.checkMinLength($value, ${schema.minLength}, $name);'; + yield "dynamite_utils.checkMinLength($value as String?, ${schema.minLength}, '$name');"; } if (schema.maxLength != null) { - yield 'dynamite_utils.checkMaxLength($value, ${schema.maxLength}, $name);'; + yield "dynamite_utils.checkMaxLength($value as String?, ${schema.maxLength}, '$name');"; } } } diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart index 7ca73885299..a717b2fcfee 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart +++ b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.dart @@ -6,6 +6,7 @@ import 'dart:typed_data'; import 'package:built_collection/built_collection.dart'; +import 'package:built_value/built_value.dart'; import 'package:built_value/json_object.dart'; import 'package:built_value/serializer.dart'; import 'package:built_value/standard_json_plugin.dart'; @@ -17,6 +18,8 @@ import 'package:meta/meta.dart'; import 'package:universal_io/io.dart'; import 'package:uri/uri.dart'; +part 'parameters.openapi.g.dart'; + class Client extends DynamiteClient { Client( super.baseURL, { @@ -51,6 +54,7 @@ class Client extends DynamiteClient { /// * [object] /// * [oneOf] /// * [anyOf] + /// * [enumPattern] /// /// Status codes: /// * 200 @@ -70,6 +74,7 @@ class Client extends DynamiteClient { final JsonObject? object, final GetOneOf? oneOf, final GetAnyOf? anyOf, + final GetEnumPattern? enumPattern, }) async { final rawResponse = $getRaw( contentString: contentString, @@ -84,6 +89,7 @@ class Client extends DynamiteClient { object: object, oneOf: oneOf, anyOf: anyOf, + enumPattern: enumPattern, ); return rawResponse.future; @@ -107,6 +113,7 @@ class Client extends DynamiteClient { /// * [object] /// * [oneOf] /// * [anyOf] + /// * [enumPattern] /// /// Status codes: /// * 200 @@ -127,6 +134,7 @@ class Client extends DynamiteClient { final JsonObject? object, final GetOneOf? oneOf, final GetAnyOf? anyOf, + final GetEnumPattern? enumPattern, }) { final parameters = {}; final headers = { @@ -180,9 +188,13 @@ class Client extends DynamiteClient { final $anyOf = jsonSerializers.serialize(anyOf, specifiedType: const FullType(GetAnyOf)); parameters['anyOf'] = $anyOf; + final $enumPattern = jsonSerializers.serialize(enumPattern, specifiedType: const FullType(GetEnumPattern)); + dynamite_utils.checkPattern($enumPattern as String?, RegExp('[a-z]'), 'enumPattern'); + parameters['enum_pattern'] = $enumPattern; + final uri = Uri.parse( UriTemplate( - '/{?content_string*,content_parameter*,array*,bool*,string*,string_binary*,int*,double*,num*,object*,oneOf*,anyOf*}', + '/{?content_string*,content_parameter*,array*,bool*,string*,string_binary*,int*,double*,num*,object*,oneOf*,anyOf*,enum_pattern*}', ).expand(parameters), ); return DynamiteRawResponse( @@ -366,6 +378,21 @@ class Client extends DynamiteClient { } } +class GetEnumPattern extends EnumClass { + const GetEnumPattern._(super.name); + + static const GetEnumPattern a = _$getEnumPatternA; + + @BuiltValueEnumConst(wireName: '0') + static const GetEnumPattern $0 = _$getEnumPattern$0; + + static BuiltSet get values => _$getEnumPatternValues; + + static GetEnumPattern valueOf(final String name) => _$valueOfGetEnumPattern(name); + + static Serializer get serializer => _$getEnumPatternSerializer; +} + typedef GetOneOf = ({bool? $bool, String? string}); typedef GetAnyOf = ({bool? $bool, String? string}); @@ -442,7 +469,8 @@ final Serializers serializers = (Serializers().toBuilder() ) ..add(ContentString.serializer) ..addBuilderFactory(const FullType(BuiltList, [FullType(String)]), ListBuilder.new) - ..add($BoolStringExtension.serializer)) + ..add($BoolStringExtension.serializer) + ..add(GetEnumPattern.serializer)) .build(); @visibleForTesting diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.g.dart b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.g.dart new file mode 100644 index 00000000000..8f8108828a1 --- /dev/null +++ b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.g.dart @@ -0,0 +1,53 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'parameters.openapi.dart'; + +// ************************************************************************** +// BuiltValueGenerator +// ************************************************************************** + +const GetEnumPattern _$getEnumPatternA = GetEnumPattern._('a'); +const GetEnumPattern _$getEnumPattern$0 = GetEnumPattern._('\$0'); + +GetEnumPattern _$valueOfGetEnumPattern(String name) { + switch (name) { + case 'a': + return _$getEnumPatternA; + case '\$0': + return _$getEnumPattern$0; + default: + throw ArgumentError(name); + } +} + +final BuiltSet _$getEnumPatternValues = BuiltSet(const [ + _$getEnumPatternA, + _$getEnumPattern$0, +]); + +Serializer _$getEnumPatternSerializer = _$GetEnumPatternSerializer(); + +class _$GetEnumPatternSerializer implements PrimitiveSerializer { + static const Map _toWire = { + '\$0': '0', + }; + static const Map _fromWire = { + '0': '\$0', + }; + + @override + final Iterable types = const [GetEnumPattern]; + @override + final String wireName = 'GetEnumPattern'; + + @override + Object serialize(Serializers serializers, GetEnumPattern object, {FullType specifiedType = FullType.unspecified}) => + _toWire[object.name] ?? object.name; + + @override + GetEnumPattern deserialize(Serializers serializers, Object serialized, + {FullType specifiedType = FullType.unspecified}) => + GetEnumPattern.valueOf(_fromWire[serialized] ?? (serialized is String ? serialized : '')); +} + +// ignore_for_file: deprecated_member_use_from_same_package,type=lint diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json index 502817b1d16..99e98e1ff3c 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json +++ b/packages/dynamite/dynamite_end_to_end_test/lib/parameters.openapi.json @@ -120,6 +120,18 @@ } ] } + }, + { + "name": "enum_pattern", + "in": "query", + "schema": { + "type": "string", + "pattern": "[a-z]", + "enum": [ + "a", + "0" + ] + } } ], "responses": { diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/pattern_check.openapi.dart b/packages/dynamite/dynamite_end_to_end_test/lib/pattern_check.openapi.dart index 27295afb667..2d344b7dcc0 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/pattern_check.openapi.dart +++ b/packages/dynamite/dynamite_end_to_end_test/lib/pattern_check.openapi.dart @@ -58,12 +58,12 @@ abstract class TestObject implements $TestObjectInterface, Built