Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Dart2 template to advertised best-practices, plenty of additions too. #7585

Merged
merged 23 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3af4527
Updated Dart2 template.
noordawod Oct 3, 2020
4270565
Generated Petstore client code for Dart2.
noordawod Oct 3, 2020
2349150
Use double-quotes instead of single-quotes, to be consistent with out…
noordawod Oct 3, 2020
c28af21
Updated Petstore samples.
noordawod Oct 3, 2020
7161f31
Wrap few more places with triple curly parentheses.
noordawod Oct 3, 2020
c8c2b62
Generated Petstore files.
noordawod Oct 3, 2020
8d62cac
Merge branch 'master' into refactor/dart2-template
noordawod Oct 4, 2020
1cde1a8
Revert to using single quotes, fix Java generator too.
noordawod Oct 4, 2020
8019aab
Generated Petstore files.
noordawod Oct 4, 2020
96ba839
Lower case the content type value.
noordawod Oct 4, 2020
e761b0b
Only lower-case content type when checking its value.
noordawod Oct 4, 2020
44e929d
Generated files from running "./bin/utils/ensure-up-to-date".
noordawod Oct 4, 2020
6a5e77b
Remove outdated comments for Dart1.
noordawod Oct 4, 2020
8578a2a
Regenerate Petstore samples for Dart.
noordawod Oct 4, 2020
a7bab6a
Added a new option "pubLibrary".
noordawod Oct 4, 2020
03c1de7
Merge branch 'master' into refactor/dart2-template
noordawod Oct 5, 2020
7eaaf6d
Merge branch 'master' into refactor/dart2-template
noordawod Oct 5, 2020
8981620
Added support for (de)serializing a DateTime into an epoch value.
noordawod Oct 6, 2020
a28ccd3
Merge branch 'master' into refactor/dart2-template
noordawod Oct 6, 2020
bcec0ba
Make client variable a getter/setter, adjust docs.
noordawod Oct 6, 2020
991fcf6
Fixed a small error in class template.
noordawod Oct 6, 2020
412df35
Fix Dart documentation in API classes.
noordawod Oct 6, 2020
c187866
Simplify test in HTTP Basic auth, remove unused lint rule.
noordawod Oct 6, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/dart-dio.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|pubAuthorEmail|Email address of the author in generated pubspec| |null|
|pubDescription|Description in generated pubspec| |null|
|pubHomepage|Homepage in generated pubspec| |null|
|pubLibrary|Library name in generated code| |null|
|pubName|Name in generated pubspec| |null|
|pubVersion|Version in generated pubspec| |null|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/dart-jaguar.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|pubAuthorEmail|Email address of the author in generated pubspec| |null|
|pubDescription|Description in generated pubspec| |null|
|pubHomepage|Homepage in generated pubspec| |null|
|pubLibrary|Library name in generated code| |null|
|pubName|Name in generated pubspec| |null|
|pubVersion|Version in generated pubspec| |null|
|serialization|Choose serialization format JSON or PROTO is supported| |null|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/dart.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|pubAuthorEmail|Email address of the author in generated pubspec| |null|
|pubDescription|Description in generated pubspec| |null|
|pubHomepage|Homepage in generated pubspec| |null|
|pubLibrary|Library name in generated code| |null|
|pubName|Name in generated pubspec| |null|
|pubVersion|Version in generated pubspec| |null|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(DartClientCodegen.class);

public static final String PUB_LIBRARY = "pubLibrary";
public static final String PUB_NAME = "pubName";
public static final String PUB_VERSION = "pubVersion";
public static final String PUB_DESCRIPTION = "pubDescription";
Expand All @@ -67,6 +68,7 @@ public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
public static final String PUB_HOMEPAGE = "pubHomepage";
public static final String USE_ENUM_EXTENSION = "useEnumExtension";

protected String pubLibrary = "openapi.api";
protected String pubName = "openapi";
protected String pubVersion = "1.0.0";
protected String pubDescription = "OpenAPI API client";
Expand Down Expand Up @@ -169,6 +171,7 @@ public DartClientCodegen() {
typeMapping.put("URI", "String");
typeMapping.put("ByteArray", "String");

cliOptions.add(new CliOption(PUB_LIBRARY, "Library name in generated code"));
cliOptions.add(new CliOption(PUB_NAME, "Name in generated pubspec"));
cliOptions.add(new CliOption(PUB_VERSION, "Version in generated pubspec"));
cliOptions.add(new CliOption(PUB_DESCRIPTION, "Description in generated pubspec"));
Expand Down Expand Up @@ -214,6 +217,13 @@ public void processOpts() {
additionalProperties.put(PUB_NAME, pubName);
}

if (additionalProperties.containsKey(PUB_LIBRARY)) {
this.setPubLibrary((String) additionalProperties.get(PUB_LIBRARY));
} else {
//not set, use to be passed to template
additionalProperties.put(PUB_LIBRARY, pubLibrary);
}

if (additionalProperties.containsKey(PUB_VERSION)) {
this.setPubVersion((String) additionalProperties.get(PUB_VERSION));
} else {
Expand Down Expand Up @@ -409,7 +419,7 @@ public String toDefaultValue(Schema schema) {

if (schema.getDefault() != null) {
if (ModelUtils.isStringSchema(schema)) {
return "\"" + schema.getDefault().toString().replaceAll("\"", "\\\"") + "\"";
return "'" + schema.getDefault().toString().replaceAll("'", "\\'") + "'";
}
return schema.getDefault().toString();
} else {
Expand Down Expand Up @@ -532,7 +542,7 @@ public String toEnumValue(String value, String datatype) {
"int".equalsIgnoreCase(datatype)) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
return "'" + escapeText(value) + "'";
}
}

Expand All @@ -548,6 +558,10 @@ public String toOperationId(String operationId) {
return camelize(operationId, true);
}

public void setPubLibrary(String pubLibrary) {
this.pubLibrary = pubLibrary;
}

public void setPubName(String pubName) {
this.pubName = pubName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public String toDefaultValue(Schema schema) {

if (schema.getDefault() != null) {
if (ModelUtils.isStringSchema(schema)) {
return "\"" + schema.getDefault().toString().replaceAll("\"", "\\\"") + "\"";
return "'" + schema.getDefault().toString().replaceAll("'", "\\'") + "'";
}
return schema.getDefault().toString();
} else {
Expand Down Expand Up @@ -179,6 +179,13 @@ public void processOpts() {

additionalProperties.put(IS_FORMAT_JSON, true);

if (additionalProperties.containsKey(PUB_LIBRARY)) {
this.setPubLibrary((String) additionalProperties.get(PUB_LIBRARY));
} else {
//not set, use to be passed to template
additionalProperties.put(PUB_LIBRARY, pubLibrary);
}

if (additionalProperties.containsKey(PUB_NAME)) {
this.setPubName((String) additionalProperties.get(PUB_NAME));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public String toDefaultValue(Schema schema) {

if (schema.getDefault() != null) {
if (ModelUtils.isStringSchema(schema)) {
return "\"" + schema.getDefault().toString().replaceAll("\"", "\\\"") + "\"";
return "'" + schema.getDefault().toString().replaceAll("'", "\\'") + "'";
}
return schema.getDefault().toString();
} else {
Expand Down Expand Up @@ -172,6 +172,13 @@ public void processOpts() {
additionalProperties.put(IS_FORMAT_PROTO, false);
}

if (additionalProperties.containsKey(PUB_LIBRARY)) {
this.setPubLibrary((String) additionalProperties.get(PUB_LIBRARY));
} else {
//not set, use to be passed to template
additionalProperties.put(PUB_LIBRARY, pubLibrary);
}

if (additionalProperties.containsKey(PUB_NAME)) {
this.setPubName((String) additionalProperties.get(PUB_NAME));
} else {
Expand Down
38 changes: 19 additions & 19 deletions modules/openapi-generator/src/main/resources/dart2/README.mustache
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# {{pubName}}
# {{{pubName}}}
{{#appDescriptionWithNewLines}}
{{{appDescriptionWithNewLines}}}
{{/appDescriptionWithNewLines}}

This Dart package is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project:

- API version: {{appVersion}}
- API version: {{{appVersion}}}
{{#artifactVersion}}
- Package version: {{artifactVersion}}
- Package version: {{{artifactVersion}}}
{{/artifactVersion}}
{{^hideGenerationTimestamp}}
- Build date: {{generatedDate}}
- Build date: {{{generatedDate}}}
{{/hideGenerationTimestamp}}
- Build package: {{generatorClass}}
- Build package: {{{generatorClass}}}
{{#infoUrl}}
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
{{/infoUrl}}
Expand All @@ -27,16 +27,16 @@ Dart 2.0 or later
If this Dart package is published to Github, add the following dependency to your pubspec.yaml
```
dependencies:
{{pubName}}:
git: https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}.git
{{{pubName}}}:
git: https://{{{gitHost}}}/{{{gitUserId}}}/{{{gitRepoId}}}.git
```

### Local
To use the package in your local drive, add the following dependency to your pubspec.yaml
```
dependencies:
{{pubName}}:
path: /path/to/{{pubName}}
{{{pubName}}}:
path: /path/to/{{{pubName}}}
```

## Tests
Expand All @@ -48,7 +48,7 @@ TODO
Please follow the [installation procedure](#installation--usage) and then run the following:

```dart
import 'package:{{pubName}}/api.dart';
import 'package:{{{pubName}}}/api.dart';
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down Expand Up @@ -80,34 +80,34 @@ import 'package:{{pubName}}/api.dart';
{{/authMethods}}
{{/hasAuthMethods}}

var api_instance = {{classname}}();
final api_instance = {{{classname}}}();
{{#allParams}}
var {{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{dataType}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
final {{{paramName}}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{{dataType}}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
{{/allParams}}

try {
{{#returnType}}var result = {{/returnType}}api_instance.{{{operationId}}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{#returnType}}final result = {{/returnType}}api_instance.{{{operationId}}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{#returnType}}
print(result);
{{/returnType}}
} catch (e) {
print("Exception when calling {{classname}}->{{operationId}}: $e\n");
print('Exception when calling {{{classname}}}->{{{operationId}}}: $e\n');
}
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
```

## Documentation for API Endpoints

All URIs are relative to *{{basePath}}*
All URIs are relative to *{{{basePath}}}*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**]({{apiDocPath}}/{{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{{classname}}}* | [**{{{operationId}}}**]({{{apiDocPath}}}/{{{classname}}}.md#{{{operationIdLowerCase}}}) | **{{{httpMethod}}}** {{{path}}} | {{#summary}}{{{summary}}}{{/summary}}
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}

## Documentation For Models

{{#models}}{{#model}} - [{{{classname}}}]({{modelDocPath}}/{{{classname}}}.md)
{{#models}}{{#model}} - [{{{classname}}}]({{{modelDocPath}}}/{{{classname}}}.md)
{{/model}}{{/models}}

## Documentation For Authorization
Expand All @@ -122,7 +122,7 @@ Class | Method | HTTP request | Description
{{/isApiKey}}
{{#isBasic}}
{{#isBasicBasic}}
- **Type**: HTTP basicc authentication
- **Type**: HTTP Basic authentication
{{/isBasicBasic}}
{{#isBasicBearer}}
- **Type**: HTTP Bearer authentication
Expand All @@ -140,6 +140,6 @@ Class | Method | HTTP request | Description

## Author

{{#apiInfo}}{{#apis}}{{^hasMore}}{{infoEmail}}
{{#apiInfo}}{{#apis}}{{^hasMore}}{{{infoEmail}}}
{{/hasMore}}{{/apis}}{{/apiInfo}}

Loading