From d103118975e7f66ec2cbf10a1f9d28ef935be6eb Mon Sep 17 00:00:00 2001 From: Uladzimir Paliukhovich Date: Fri, 1 Dec 2023 15:24:35 +0300 Subject: [PATCH] Fixed formurlencoded requests --- .../swagger_requests_generator.dart | 157 +++++++++++------- .../requests/swagger_request.dart | 12 +- 2 files changed, 108 insertions(+), 61 deletions(-) diff --git a/lib/src/code_generators/swagger_requests_generator.dart b/lib/src/code_generators/swagger_requests_generator.dart index 563ddd9b..12ff685a 100644 --- a/lib/src/code_generators/swagger_requests_generator.dart +++ b/lib/src/code_generators/swagger_requests_generator.dart @@ -189,6 +189,9 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { ['post', 'put', 'patch'].contains(requestType) && swaggerRequest.parameters.none((p) => p.inParameter == kBody); + final isFormUrlEncoded = + swaggerRequest.requestBody?.content?.isFormUrlEncoded ?? false; + final isMultipart = parameters.any((p) { return p.annotations .any((p0) => p0.call([]).toString().contains('symbol=Part')); @@ -208,7 +211,12 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { )) ..name = methodName ..annotations.addAll(_getMethodAnnotation( - requestType, annotationPath, hasOptionalBody, isMultipart)) + requestType, + annotationPath, + hasOptionalBody, + isMultipart, + isFormUrlEncoded, + )) ..returns = Reference(returns)); final allModels = _getAllMethodModels( @@ -468,7 +476,23 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { String path, bool hasOptionalBody, bool isMultipart, + bool isFormUrlEncoded, ) { + if (isFormUrlEncoded) { + return [ + refer(requestType.pascalCase).call( + [], + { + kPath: literalString(path), + 'headers': refer('{contentTypeKey: formEncodedHeaders}'), + }, + ), + refer('FactoryConverter').call( + [refer('request: FormUrlEncodedConverter.requestFactory')], + {}, + ), + ]; + } return [ refer(requestType.pascalCase).call( [], @@ -767,8 +791,23 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { final requestBody = swaggerRequest.requestBody; if (requestBody != null) { - // MULTIPART REQUESTS - if (requestBody.content?.isMultipart == true) { + // FORM URLENCODED + if (requestBody.content?.isFormUrlEncoded == true) { + result.add( + Parameter( + (p) => p + ..name = kBody + ..named = true + ..required = true + ..type = Reference(kMapStringDynamic) + ..named = true + ..annotations.add( + refer(kPart.pascalCase).call([]), + ), + ), + ); + } else if (requestBody.content?.isMultipart == true) { + // MULTIPART REQUESTS var schema = requestBody.content?.schema; if (schema?.ref.isNotEmpty == true) { @@ -864,70 +903,70 @@ class SwaggerRequestsGenerator extends SwaggerGeneratorBase { }); return result.distinctParameters(); - } + } else { +// OTHER REQUESTS EXCEPT MULTIPART + var typeName = ''; + + if (requestBody.hasRef) { + final ref = requestBody.ref; + typeName = ref.getRef(); + + if (root.components?.requestBodies + .containsKey(ref.getUnformattedRef()) == + true) { + typeName = getValidatedClassName( + '${ref.getUnformattedRef()}\$RequestBody'); + } - // OTHER REQUESTS EXCEPT MULTIPART - var typeName = ''; + final requestBodyRef = + root.components?.requestBodies[ref.getRef()]?.ref ?? ''; - if (requestBody.hasRef) { - final ref = requestBody.ref; - typeName = ref.getRef(); + if (requestBodyRef.isNotEmpty) { + typeName = requestBodyRef.getRef(); + } - if (root.components?.requestBodies - .containsKey(ref.getUnformattedRef()) == - true) { - typeName = - getValidatedClassName('${ref.getUnformattedRef()}\$RequestBody'); + typeName = getValidatedClassName(typeName); } - final requestBodyRef = - root.components?.requestBodies[ref.getRef()]?.ref ?? ''; - - if (requestBodyRef.isNotEmpty) { - typeName = requestBodyRef.getRef(); + final schema = requestBody.content?.schema; + + if (schema != null) { + if (schema.format == kBinary || schema.oneOf.isNotEmpty) { + typeName = kObject.pascalCase; + } else if (schema.items?.type.isNotEmpty == true) { + typeName = _mapParameterName(schema.items!.type, + schema.items!.format, options.modelPostfix) + .asList(); + } else if (schema.allOf.length == 1 && + schema.allOf.first.ref.isNotEmpty) { + typeName = getValidatedClassName(schema.allOf.first.ref.getRef()); + } else { + typeName = _getRequestBodyTypeName( + schema: schema, + modelPostfix: options.modelPostfix, + root: root, + requestPath: path + requestType.pascalCase, + ); + } } - typeName = getValidatedClassName(typeName); - } - - final schema = requestBody.content?.schema; - - if (schema != null) { - if (schema.format == kBinary || schema.oneOf.isNotEmpty) { - typeName = kObject.pascalCase; - } else if (schema.items?.type.isNotEmpty == true) { - typeName = _mapParameterName(schema.items!.type, schema.items!.format, - options.modelPostfix) - .asList(); - } else if (schema.allOf.length == 1 && - schema.allOf.first.ref.isNotEmpty) { - typeName = getValidatedClassName(schema.allOf.first.ref.getRef()); - } else { - typeName = _getRequestBodyTypeName( - schema: schema, - modelPostfix: options.modelPostfix, - root: root, - requestPath: path + requestType.pascalCase, - ); - } + result.add( + Parameter( + (p) => p + ..name = kBody + ..named = true + ..required = true + ..type = Reference( + (typeName.isNotEmpty ? typeName : kObject.pascalCase) + .makeNullable(), + ) + ..named = true + ..annotations.add( + refer(kBody.pascalCase).call([]), + ), + ), + ); } - - result.add( - Parameter( - (p) => p - ..name = kBody - ..named = true - ..required = true - ..type = Reference( - (typeName.isNotEmpty ? typeName : kObject.pascalCase) - .makeNullable(), - ) - ..named = true - ..annotations.add( - refer(kBody.pascalCase).call([]), - ), - ), - ); } return result.distinctParameters(); diff --git a/lib/src/swagger_models/requests/swagger_request.dart b/lib/src/swagger_models/requests/swagger_request.dart index cabdd228..6c2baecf 100644 --- a/lib/src/swagger_models/requests/swagger_request.dart +++ b/lib/src/swagger_models/requests/swagger_request.dart @@ -85,7 +85,9 @@ RequestContent? _contentFromJson(Map? map) { final multipart = map['multipart/form-data']['schema'] as Map; return RequestContent( - isMultipart: true, schema: SwaggerSchema.fromJson(multipart)); + isFormUrlEncoded: true, + schema: SwaggerSchema.fromJson(multipart), + ); } if (map.containsKey('application/x-www-form-urlencoded') && @@ -94,7 +96,10 @@ RequestContent? _contentFromJson(Map? map) { final multipart = map['application/x-www-form-urlencoded']['schema'] as Map; return RequestContent( - isMultipart: true, schema: SwaggerSchema.fromJson(multipart)); + isMultipart: true, + isFormUrlEncoded: true, + schema: SwaggerSchema.fromJson(multipart), + ); } final content = map.values.first as Map; @@ -106,12 +111,15 @@ RequestContent? _contentFromJson(Map? map) { class RequestContent { RequestContent({ this.isMultipart, + this.isFormUrlEncoded = false, this.schema, }); @JsonKey(name: 'schema') final SwaggerSchema? schema; + final bool isFormUrlEncoded; + final bool? isMultipart; Map toJson() => _$RequestContentToJson(this);