swagger-api / swagger-api/swagger-codegen

[Dart] code has compile issues when 'browserClient' = false

Open
#8,658 1 comment 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

The Dart code has a couple of issues when it is generated. I have some suggestions which work for me in Flutter.

Swagger-codegen version

Version is 2.4.0

Swagger declaration file content or url

File 1 with compile problems:
modules/swagger-codegen/src/main/resources/dart/auth/http_basic_auth.mustache

    headerParams["Authorization"] = "Basic " + **BASE64**.encode(UTF8.encode(str));

File 2 with compile problems:
modules/swagger-codegen/src/main/resources/dart/api_client.mustache

var decodedJson = **JSON**.decode(json);
serialized = **JSON**.encode(obj);
Command line used for generation

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i http://petstore.swagger.io/v2/swagger.json -l dart -c flutter.config.json -o samples/client/petstore/dart

Steps to reproduce

Create a config file:
flutter.config.json

{
	"browserClient": false
}

run the command above and the two files will have compile errors unless the changes below are made to the mustache files.

Suggest a fix/enhancement

File 1 to change:
modules/swagger-codegen/src/main/resources/dart/auth/http_basic_auth.mustache

  @override
  void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
    String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);{{#browserClient}}
    headerParams["Authorization"] = "Basic " + BASE64.encode(UTF8.encode(str));{{/browserClient}}{{^browserClient}}
    headerParams["Authorization"] = "Basic " + base64.encode(utf8.encode(str));{{/browserClient}}
  }

File 2 to change:
modules/swagger-codegen/src/main/resources/dart/api_client.mustache
Change 1:

    if (targetType == 'String') return json;

{{#browserClient}}    var decodedJson = JSON.decode(json);{{/browserClient}}
{{^browserClient}}    var decodedJson = jsonDecode(json);{{/browserClient}}
    return _deserialize(decodedJson, targetType);
  }

Change 2:

  String serialize(Object obj) {
    String serialized = '';
    if (obj == null) {
      serialized = '';
    } else {
{{#browserClient}}      serialized = JSON.encode(obj);{{/browserClient}}
{{^browserClient}}      serialized = json.encode(obj);{{/browserClient}}
    }
    return serialized;
  }

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/auth/http_basic_auth.mustache and modules/swagger-codegen/src/main/resources/dart/api_client.mustache. Generate the Dart client with flutter.config.json setting browserClient to false, then verify the generated files compile and use the non-browser APIs shown in the issue.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.