swagger-api / swagger-api/swagger-codegen
[Dart] code has compile issues when 'browserClient' = false
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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