Skip to content

Commit

Permalink
Fixed formurlencoded requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Uladzimir Paliukhovich committed Dec 1, 2023
1 parent b050bf6 commit d103118
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 61 deletions.
157 changes: 98 additions & 59 deletions lib/src/code_generators/swagger_requests_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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(
Expand Down Expand Up @@ -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(
[],
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions lib/src/swagger_models/requests/swagger_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ RequestContent? _contentFromJson(Map<String, dynamic>? map) {
final multipart =
map['multipart/form-data']['schema'] as Map<String, dynamic>;
return RequestContent(
isMultipart: true, schema: SwaggerSchema.fromJson(multipart));
isFormUrlEncoded: true,
schema: SwaggerSchema.fromJson(multipart),
);
}

if (map.containsKey('application/x-www-form-urlencoded') &&
Expand All @@ -94,7 +96,10 @@ RequestContent? _contentFromJson(Map<String, dynamic>? map) {
final multipart = map['application/x-www-form-urlencoded']['schema']
as Map<String, dynamic>;
return RequestContent(
isMultipart: true, schema: SwaggerSchema.fromJson(multipart));
isMultipart: true,
isFormUrlEncoded: true,
schema: SwaggerSchema.fromJson(multipart),
);
}

final content = map.values.first as Map<String, dynamic>;
Expand All @@ -106,12 +111,15 @@ RequestContent? _contentFromJson(Map<String, dynamic>? map) {
class RequestContent {
RequestContent({
this.isMultipart,
this.isFormUrlEncoded = false,
this.schema,
});

@JsonKey(name: 'schema')
final SwaggerSchema? schema;

final bool isFormUrlEncoded;

final bool? isMultipart;

Map<String, dynamic> toJson() => _$RequestContentToJson(this);
Expand Down

0 comments on commit d103118

Please sign in to comment.