swagger-api / swagger-api/swagger-codegen

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

Open
#8,424 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/dart/api_client.mustache at the referenced query-string construction. Generate the minimal api.yaml with the documented Java command, then run the Dart reproduction using a value containing a plus sign. Done means the generated request uses percent-encoded query components, such as %2B for +.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.