Skip to content

Commit f34f15c

Browse files
committed
generator for dart jaguar
1 parent 5b7be9e commit f34f15c

File tree

170 files changed

+19497
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+19497
-11
lines changed

bin/dart-jaguar-petstore.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
30+
# Generate client
31+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/dart-jaguar -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l dart-jaguar -o samples/client/petstore/dart-jaguar/swagger -DhideGenerationTimestamp=true"
32+
java $JAVA_OPTS -jar $executable $ags
33+
34+
# Generate non-browserClient and put it to the flutter sample app
35+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/dart-jaguar -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l dart-jaguar -o samples/client/petstore/dart-jaguar/flutter_petstore/swagger -DhideGenerationTimestamp=true"
36+
java $JAVA_OPTS -jar $executable $ags
37+
38+
# There is a proposal to allow importing different libraries depending on the environment:
39+
# https://github.com/munificent/dep-interface-libraries
40+
# When this is implemented there will only be one library.
41+
42+
# The current petstore test will then work for both: the browser library and the vm library.
43+

bin/windows/dart-client-petstore.bat

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
8+
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l dart-jaguar -o samples\client\petstore\dart-jaguar\swagger -DhideGenerationTimestamp=true -DbrowserClient=false
9+
java %JAVA_OPTS% -jar %executable% %ags%
10+
11+
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l dart-jaguar -o samples\client\petstore\dart-jaguar\flutter_petstore\swagger -DhideGenerationTimestamp=true
12+
java %JAVA_OPTS% -jar %executable% %ags%
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package io.swagger.codegen.languages;
2+
3+
import com.samskivert.mustache.Mustache;
4+
import com.samskivert.mustache.Template;
5+
import io.swagger.codegen.*;
6+
import io.swagger.models.properties.ArrayProperty;
7+
import io.swagger.models.properties.MapProperty;
8+
import io.swagger.models.properties.Property;
9+
import org.apache.commons.lang3.StringUtils;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.io.StringWriter;
14+
import java.io.Writer;
15+
import java.util.HashSet;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.Set;
19+
20+
public class DartJaguarClientCodegen extends DartClientCodegen {
21+
private static Set<String> modelToIgnore = new HashSet<>();
22+
static {
23+
modelToIgnore.add("datetime");
24+
modelToIgnore.add("list");
25+
modelToIgnore.add("map");
26+
modelToIgnore.add("file");
27+
}
28+
public DartJaguarClientCodegen() {
29+
super();
30+
browserClient = false;
31+
outputFolder = "generated-code/dart-jaguar";
32+
embeddedTemplateDir = templateDir = "dart-jaguar";
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return "dart-jaguar";
38+
}
39+
40+
@Override
41+
public String getHelp() {
42+
return "Generates a Dart Jaguar client library.";
43+
}
44+
45+
@Override
46+
public String toDefaultValue(Property p) {
47+
if (p instanceof MapProperty) {
48+
return "const {}";
49+
} else if (p instanceof ArrayProperty) {
50+
return "const []";
51+
}
52+
return super.toDefaultValue(p);
53+
}
54+
55+
@Override
56+
public void processOpts() {
57+
if (additionalProperties.containsKey(PUB_NAME)) {
58+
this.setPubName((String) additionalProperties.get(PUB_NAME));
59+
} else {
60+
//not set, use to be passed to template
61+
additionalProperties.put(PUB_NAME, pubName);
62+
}
63+
64+
if (additionalProperties.containsKey(PUB_VERSION)) {
65+
this.setPubVersion((String) additionalProperties.get(PUB_VERSION));
66+
} else {
67+
//not set, use to be passed to template
68+
additionalProperties.put(PUB_VERSION, pubVersion);
69+
}
70+
71+
if (additionalProperties.containsKey(PUB_DESCRIPTION)) {
72+
this.setPubDescription((String) additionalProperties.get(PUB_DESCRIPTION));
73+
} else {
74+
//not set, use to be passed to template
75+
additionalProperties.put(PUB_DESCRIPTION, pubDescription);
76+
}
77+
78+
if (additionalProperties.containsKey(USE_ENUM_EXTENSION)) {
79+
this.setUseEnumExtension(convertPropertyToBooleanAndWriteBack(USE_ENUM_EXTENSION));
80+
} else {
81+
// Not set, use to be passed to template.
82+
additionalProperties.put(USE_ENUM_EXTENSION, useEnumExtension);
83+
}
84+
85+
if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
86+
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
87+
}
88+
89+
// make api and model doc path available in mustache template
90+
additionalProperties.put("apiDocPath", apiDocPath);
91+
additionalProperties.put("modelDocPath", modelDocPath);
92+
93+
final String libFolder = sourceFolder + File.separator + "lib";
94+
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
95+
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", ".analysis_options"));
96+
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));
97+
98+
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
99+
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
100+
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
101+
}
102+
103+
104+
@Override
105+
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
106+
objs = super.postProcessModels(objs);
107+
List<Object> models = (List<Object>) objs.get("models");
108+
for (Object _mo : models) {
109+
Map<String, Object> mo = (Map<String, Object>) _mo;
110+
Set<String> modelImports = new HashSet<>();
111+
CodegenModel cm = (CodegenModel) mo.get("model");
112+
for (String modelImport : cm.imports) {
113+
if(!modelToIgnore.contains(modelImport.toLowerCase())) {
114+
modelImports.add(underscore(modelImport));
115+
}
116+
}
117+
cm.imports = modelImports;
118+
cm.vendorExtensions.put("hasVars", cm.vars.size() > 0);
119+
}
120+
//objs.put("modelImports", modelImports);
121+
return objs;
122+
}
123+
124+
@Override
125+
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
126+
objs = super.postProcessOperations(objs);
127+
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
128+
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
129+
130+
Set<String> modelImports = new HashSet<>();
131+
132+
for (CodegenOperation op : operationList) {
133+
op.httpMethod = StringUtils.capitalize(op.httpMethod.toLowerCase());
134+
boolean isJson = true; //default to JSON
135+
boolean isForm = false;
136+
boolean isMultipart = false;
137+
if(op.consumes != null) {
138+
for (Map<String, String> consume : op.consumes) {
139+
if (consume.containsKey("mediaType")) {
140+
String type = consume.get("mediaType");
141+
isJson = type.equalsIgnoreCase("application/json");
142+
isForm = type.equalsIgnoreCase("application/x-www-form-urlencoded");
143+
isMultipart = type.equalsIgnoreCase("multipart/form-data");
144+
break;
145+
}
146+
}
147+
}
148+
149+
op.vendorExtensions.put("isJson", isJson);
150+
op.vendorExtensions.put("isForm", isForm);
151+
op.vendorExtensions.put("isMultipart", isMultipart);
152+
153+
Set<String> imports = new HashSet<>();
154+
for (String item : op.imports) {
155+
if(!modelToIgnore.contains(item.toLowerCase())) {
156+
imports.add(underscore(item));
157+
}
158+
}
159+
modelImports.addAll(imports);
160+
op.imports = imports;
161+
162+
String[] items = op.path.split("/", -1);
163+
String jaguarPath = "";
164+
165+
for (int i = 0; i < items.length; ++i) {
166+
if (items[i].matches("^\\{(.*)\\}$")) { // wrap in {}
167+
jaguarPath = jaguarPath + ":" + items[i].replace("{", "").replace("}", "");
168+
} else {
169+
jaguarPath = jaguarPath + items[i];
170+
}
171+
172+
if (i != items.length -1) {
173+
jaguarPath = jaguarPath + "/";
174+
}
175+
}
176+
177+
op.path = jaguarPath;
178+
}
179+
180+
objs.put("modelImports", modelImports);
181+
182+
return objs;
183+
}
184+
}

modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ io.swagger.codegen.languages.ConfluenceWikiGenerator
1212
io.swagger.codegen.languages.CppRestClientCodegen
1313
io.swagger.codegen.languages.CsharpDotNet2ClientCodegen
1414
io.swagger.codegen.languages.DartClientCodegen
15+
io.swagger.codegen.languages.DartJaguarClientCodegen
1516
io.swagger.codegen.languages.ElixirClientCodegen
1617
io.swagger.codegen.languages.ElmClientCodegen
1718
io.swagger.codegen.languages.EiffelClientCodegen
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# {{pubName}}
2+
{{#appDescription}}
3+
{{{appDescription}}}
4+
{{/appDescription}}
5+
6+
This Dart package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
7+
8+
- API version: {{appVersion}}
9+
{{#artifactVersion}}
10+
- Package version: {{artifactVersion}}
11+
{{/artifactVersion}}
12+
{{^hideGenerationTimestamp}}
13+
- Build date: {{generatedDate}}
14+
{{/hideGenerationTimestamp}}
15+
- Build package: {{generatorClass}}
16+
{{#infoUrl}}
17+
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
18+
{{/infoUrl}}
19+
20+
## Requirements
21+
22+
Dart 2 or later OR Flutter 0.7.0 or later.
23+
24+
Once your code is generated, you need to run the build_runner command to let Jaguar implement your API:
25+
26+
```sh
27+
flutter packages pub run build_runner build
28+
or
29+
pub run build_runner build
30+
```
31+
32+
## Installation & Usage
33+
34+
### Github
35+
If this Dart package is published to Github, please include the following in pubspec.yaml
36+
```
37+
name: {{pubName}}
38+
version: {{pubVersion}}
39+
description: {{pubDescription}}
40+
dependencies:
41+
{{pubName}}:
42+
git: https://github.com/{{gitUserId}}/{{gitRepoId}}.git
43+
version: 'any'
44+
```
45+
46+
### Local
47+
To use the package in your local drive, please include the following in pubspec.yaml
48+
```
49+
dependencies:
50+
{{pubName}}:
51+
path: /path/to/{{pubName}}
52+
```
53+
54+
## Tests
55+
56+
TODO
57+
58+
## Getting Started
59+
60+
Please follow the [installation procedure](#installation--usage) and then run the following:
61+
62+
```dart
63+
import 'package:{{pubName}}/api.dart';
64+
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
65+
{{#hasAuthMethods}}
66+
{{#authMethods}}
67+
{{#isBasic}}
68+
// TODO Configure HTTP basic authorization: {{{name}}}
69+
//{{pubName}}.api.Configuration.username = 'YOUR_USERNAME';
70+
//{{pubName}}.api.Configuration.password = 'YOUR_PASSWORD';
71+
{{/isBasic}}
72+
{{#isApiKey}}
73+
// TODO Configure API key authorization: {{{name}}}
74+
//{{pubName}}.api.Configuration.apiKey{'{{{keyParamName}}}'} = 'YOUR_API_KEY';
75+
// uncomment below to setup prefix (e.g. Bearer) for API key, if needed
76+
//{{pubName}}.api.Configuration.apiKeyPrefix{'{{{keyParamName}}}'} = "Bearer";
77+
{{/isApiKey}}
78+
{{#isOAuth}}
79+
// TODO Configure OAuth2 access token for authorization: {{{name}}}
80+
//{{pubName}}.api.Configuration.accessToken = 'YOUR_ACCESS_TOKEN';
81+
{{/isOAuth}}
82+
{{/authMethods}}
83+
{{/hasAuthMethods}}
84+
85+
final swaggerGen = SwaggerGen();
86+
var api_instance = swaggerGen.get{{classname}}();
87+
{{#allParams}}
88+
var {{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}new {{dataType}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
89+
{{/allParams}}
90+
91+
try {
92+
{{#returnType}}var result = {{/returnType}}api_instance.{{{operationId}}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
93+
{{#returnType}}
94+
print(result);
95+
{{/returnType}}
96+
} catch (e) {
97+
print("Exception when calling {{classname}}->{{operationId}}: $e\n");
98+
}
99+
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
100+
```
101+
102+
## Documentation for API Endpoints
103+
104+
All URIs are relative to *{{basePath}}*
105+
106+
Class | Method | HTTP request | Description
107+
------------ | ------------- | ------------- | -------------
108+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**]({{apiDocPath}}/{{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
109+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
110+
111+
## Documentation For Models
112+
113+
{{#models}}{{#model}} - [{{{classname}}}]({{modelDocPath}}/{{{classname}}}.md)
114+
{{/model}}{{/models}}
115+
116+
## Documentation For Authorization
117+
118+
{{^authMethods}} All endpoints do not require authorization.
119+
{{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}}
120+
{{#authMethods}}## {{{name}}}
121+
122+
{{#isApiKey}}- **Type**: API key
123+
- **API key parameter name**: {{{keyParamName}}}
124+
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
125+
{{/isApiKey}}
126+
{{#isBasic}}- **Type**: HTTP basic authentication
127+
{{/isBasic}}
128+
{{#isOAuth}}- **Type**: OAuth
129+
- **Flow**: {{{flow}}}
130+
- **Authorization URL**: {{{authorizationUrl}}}
131+
- **Scopes**: {{^scopes}}N/A{{/scopes}}
132+
{{#scopes}} - **{{{scope}}}**: {{{description}}}
133+
{{/scopes}}
134+
{{/isOAuth}}
135+
136+
{{/authMethods}}
137+
138+
## Author
139+
140+
{{#apiInfo}}{{#apis}}{{^hasMore}}{{infoEmail}}
141+
{{/hasMore}}{{/apis}}{{/apiInfo}}
142+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
analyzer:
2+
strong-mode: true

0 commit comments

Comments
 (0)