swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Dart] Query parameters are not URI encoded by generated client code

Open mtter opened this issue 8 years ago • 0 comments

Description

Dart client code generated from a specification including query parameters does not URI encode these parameters when constructing the URI. URI encoding should be handled by the generated code in my opinion, because how the parameters are sent over the network should be transparent to the user of the client.

Swagger-codegen version

I'm experiencing the problem with 2.4-SNAPSHOT, but it can also be reproduced using version 2.3.1 (http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar) or by pasting the definition below into https://editor.swagger.io/ and exporting a Dart client.

Swagger declaration file content or url

This is a minimal example that demonstrates the problem:

swagger: '2.0'
info:
  version: 1.0.0
  title: Minimal example for lack of URI encoding of query parameters in Dart
paths:
  /queryParameterDemo:
    get:
      summary: Endpoint using a URL query parameter
      parameters:
        - in: query
          name: myQueryParameter
          type: string
      responses:
        200:
          description: Success
Command line used for generation

Client code can be generated using

java -jar swagger-codegen-cli-2.3.1.jar generate -l dart -i api.yaml
Steps to reproduce

Reproduction:

  • Generate client using specification and Swagger version described above.
  • Use the generated client to call GET /queryParameterDemo with a parameter including special characters that should be URL encoded e.g. paramerValue+IncludingPlusSign.

This is a small test sample that does this:

import 'package:dart/api.dart';
import 'package:test/test.dart';

void main() {
  test('Url encoding in generated client', () async {
    final api = new DefaultApi();
    await api.queryParameterDemoGet(myQueryParameter: 'paramerValue+IncludingPlusSign');
  });
}

You can now set a breakpoint in the generated ApiClient, on the line where get is invoked on the native Client:

return client.get(url, headers: headerParams);

You can verify that the value of the url parameter is https://localhost/queryParameterDemo?myQueryParameter=paramerValue+IncludingPlusSign, but should be https://localhost/queryParameterDemo?myQueryParameter=paramerValue%2BIncludingPlusSign at this point.

Related issues/PRs

None

Suggest a fix/enhancement

My suggesetion would be to use Uri.encodeQueryComponent to encode each parameter when constructing the query string in the ApiClient template:

https://github.com/swagger-api/swagger-codegen/blob/3ecf62a25d4334cb24e67e0de63b585b4bc6f69f/modules/swagger-codegen/src/main/resources/dart/api_client.mustache#L112

mtter avatar Jul 13 '18 16:07 mtter