From bca80c0d3d3f8c0c67738ef34c545a9365b982e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Sz=C3=A9kelyi?= Date: Thu, 15 Jun 2023 01:45:19 -0400 Subject: [PATCH] [dart-dio] Correctly serialize path parameters (#14666) --- .../languages/DartDioClientCodegen.java | 26 +++++++++---------- .../resources/dart/libraries/dio/api.mustache | 2 +- .../lib/src/api/pet_api.dart | 10 +++---- .../lib/src/api/store_api.dart | 5 ++-- .../lib/src/api/user_api.dart | 6 ++--- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java index 89f13abff95..dc71ca39d35 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java @@ -83,7 +83,7 @@ public class DartDioClientCodegen extends AbstractDartCodegen { .includeClientModificationFeatures( ClientModificationFeature.Authorizations, ClientModificationFeature.UserAgent - ).includeSchemaSupportFeatures( + ).includeSchemaSupportFeatures( SchemaSupportFeature.Polymorphism, SchemaSupportFeature.Union, SchemaSupportFeature.Composite, @@ -277,7 +277,7 @@ public class DartDioClientCodegen extends AbstractDartCodegen { imports.put("Uint8List", "dart:typed_data"); imports.put("MultipartFile", DIO_IMPORT); } - + private void configureDateLibrary(String srcFolder) { switch (dateLibrary) { case DATE_LIBRARY_TIME_MACHINE: @@ -364,12 +364,12 @@ public class DartDioClientCodegen extends AbstractDartCodegen { } } - for (CodegenModel model : allModels.values()) { + for (CodegenModel model : allModels.values()) { syncRootTypesWithInnerVars(allModels, model); } } private void syncRootTypesWithInnerVars(Map objs, CodegenModel model) { - List allVars = new ArrayList<>(); + List allVars = new ArrayList<>(); allVars.addAll(((Collection) model.vendorExtensions.get(kSelfAndAncestorOnlyProps))); allVars.addAll(((Collection) model.vendorExtensions.get(kSelfOnlyProps))); allVars.addAll(((Collection) model.vendorExtensions.get(kAncestorOnlyProps))); @@ -383,8 +383,8 @@ public class DartDioClientCodegen extends AbstractDartCodegen { //fill the property's VendorExtensions with the type's VendorExtensions prop.getVendorExtensions().put(kIsParent, relatedModel.getVendorExtensions().get(kIsParent)); prop.isEnum = relatedModel.isEnum; - - } + + } } } private final String kIsChild = "x-is-child"; @@ -397,7 +397,7 @@ public class DartDioClientCodegen extends AbstractDartCodegen { private final String kSelfAndAncestorOnlyProps = "x-self-and-ancestor-only-props"; private final String kHasSelfAndAncestorOnlyProps = "x-has-self-and-ancestor-only-props"; private final String kParentDiscriminator = "x-parent-discriminator"; - + // adapts codegen models and property to dart rules of inheritance private void adaptToDartInheritance(Map objs) { // get all models @@ -425,7 +425,7 @@ public class DartDioClientCodegen extends AbstractDartCodegen { allAncestorsForAllModelsFlat.addAll(allAncestors); } - + Set allPureClasses = new HashSet<>(); // set isChild,isParent,isPure @@ -546,11 +546,11 @@ public class DartDioClientCodegen extends AbstractDartCodegen { } } - /// override the default behavior of createDiscriminator - /// to remove extra mappings added as a side effect of setLegacyDiscriminatorBehavior(false) + /// override the default behavior of createDiscriminator + /// to remove extra mappings added as a side effect of setLegacyDiscriminatorBehavior(false) /// this ensures 1-1 schema mapping instead of 1-many @Override - protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema, OpenAPI openAPI) { + protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema, OpenAPI openAPI) { CodegenDiscriminator sub = super.createDiscriminator(schemaName, schema, openAPI); Discriminator originalDiscriminator = schema.getDiscriminator(); if (originalDiscriminator!=null) { @@ -566,7 +566,7 @@ public class DartDioClientCodegen extends AbstractDartCodegen { } } } - } + } return sub; } @@ -674,7 +674,7 @@ public class DartDioClientCodegen extends AbstractDartCodegen { } } - if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library) && (op.getHasFormParams() || op.getHasQueryParams())) { + if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library) && (op.getHasFormParams() || op.getHasQueryParams() || op.getHasPathParams())) { resultImports.add("package:" + pubName + "/" + sourceFolder + "/api_util.dart"); } diff --git a/modules/openapi-generator/src/main/resources/dart/libraries/dio/api.mustache b/modules/openapi-generator/src/main/resources/dart/libraries/dio/api.mustache index 113ad271ce8..5e315387a74 100644 --- a/modules/openapi-generator/src/main/resources/dart/libraries/dio/api.mustache +++ b/modules/openapi-generator/src/main/resources/dart/libraries/dio/api.mustache @@ -51,7 +51,7 @@ class {{classname}} { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'{{{path}}}'{{#pathParams}}.replaceAll('{' r'{{{baseName}}}' '}', {{{paramName}}}.toString()){{/pathParams}}; + final _path = r'{{{path}}}'{{#pathParams}}.replaceAll('{' r'{{{baseName}}}' '}', {{#includeLibraryTemplate}}api/query_param{{/includeLibraryTemplate}}.toString()){{/pathParams}}; final _options = Options( method: r'{{#lambda.uppercase}}{{httpMethod}}{{/lambda.uppercase}}', {{#isResponseFile}} diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/pet_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/pet_api.dart index 69f824c761d..8b7294d8d83 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/pet_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/pet_api.dart @@ -117,7 +117,7 @@ class PetApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/pet/{petId}'.replaceAll('{' r'petId' '}', petId.toString()); + final _path = r'/pet/{petId}'.replaceAll('{' r'petId' '}', encodeQueryParameter(_serializers, petId, const FullType(int)).toString()); final _options = Options( method: r'DELETE', headers: { @@ -341,7 +341,7 @@ class PetApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/pet/{petId}'.replaceAll('{' r'petId' '}', petId.toString()); + final _path = r'/pet/{petId}'.replaceAll('{' r'petId' '}', encodeQueryParameter(_serializers, petId, const FullType(int)).toString()); final _options = Options( method: r'GET', headers: { @@ -499,7 +499,7 @@ class PetApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/pet/{petId}'.replaceAll('{' r'petId' '}', petId.toString()); + final _path = r'/pet/{petId}'.replaceAll('{' r'petId' '}', encodeQueryParameter(_serializers, petId, const FullType(int)).toString()); final _options = Options( method: r'POST', headers: { @@ -577,7 +577,7 @@ class PetApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/pet/{petId}/uploadImage'.replaceAll('{' r'petId' '}', petId.toString()); + final _path = r'/pet/{petId}/uploadImage'.replaceAll('{' r'petId' '}', encodeQueryParameter(_serializers, petId, const FullType(int)).toString()); final _options = Options( method: r'POST', headers: { @@ -683,7 +683,7 @@ class PetApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/fake/{petId}/uploadImageWithRequiredFile'.replaceAll('{' r'petId' '}', petId.toString()); + final _path = r'/fake/{petId}/uploadImageWithRequiredFile'.replaceAll('{' r'petId' '}', encodeQueryParameter(_serializers, petId, const FullType(int)).toString()); final _options = Options( method: r'POST', headers: { diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/store_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/store_api.dart index 02d567ba40f..751978e6198 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/store_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/store_api.dart @@ -8,6 +8,7 @@ import 'package:built_value/serializer.dart'; import 'package:dio/dio.dart'; import 'package:built_collection/built_collection.dart'; +import 'package:openapi/src/api_util.dart'; import 'package:openapi/src/model/order.dart'; class StoreApi { @@ -41,7 +42,7 @@ class StoreApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/store/order/{order_id}'.replaceAll('{' r'order_id' '}', orderId.toString()); + final _path = r'/store/order/{order_id}'.replaceAll('{' r'order_id' '}', encodeQueryParameter(_serializers, orderId, const FullType(String)).toString()); final _options = Options( method: r'DELETE', headers: { @@ -168,7 +169,7 @@ class StoreApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/store/order/{order_id}'.replaceAll('{' r'order_id' '}', orderId.toString()); + final _path = r'/store/order/{order_id}'.replaceAll('{' r'order_id' '}', encodeQueryParameter(_serializers, orderId, const FullType(int)).toString()); final _options = Options( method: r'GET', headers: { diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/user_api.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/user_api.dart index 9c8f7c15a56..102f490f5e5 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/user_api.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/api/user_api.dart @@ -243,7 +243,7 @@ class UserApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/user/{username}'.replaceAll('{' r'username' '}', username.toString()); + final _path = r'/user/{username}'.replaceAll('{' r'username' '}', encodeQueryParameter(_serializers, username, const FullType(String)).toString()); final _options = Options( method: r'DELETE', headers: { @@ -290,7 +290,7 @@ class UserApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/user/{username}'.replaceAll('{' r'username' '}', username.toString()); + final _path = r'/user/{username}'.replaceAll('{' r'username' '}', encodeQueryParameter(_serializers, username, const FullType(String)).toString()); final _options = Options( method: r'GET', headers: { @@ -492,7 +492,7 @@ class UserApi { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final _path = r'/user/{username}'.replaceAll('{' r'username' '}', username.toString()); + final _path = r'/user/{username}'.replaceAll('{' r'username' '}', encodeQueryParameter(_serializers, username, const FullType(String)).toString()); final _options = Options( method: r'PUT', headers: {