Skip to content

Commit

Permalink
migrate to openapi-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored and jaumard committed Sep 21, 2018
1 parent 0d79d71 commit 012247b
Show file tree
Hide file tree
Showing 110 changed files with 114 additions and 5,650 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@

package org.openapitools.codegen.languages;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import io.swagger.models.Model;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.ModelUtils;

Expand All @@ -27,22 +35,22 @@
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class DartJaguarClientCodegen extends DartClientCodegen {
private static Set<String> modelToIgnore = new HashSet<>();

static {
modelToIgnore.add("datetime");
modelToIgnore.add("map");
modelToIgnore.add("list");
modelToIgnore.add("map");
modelToIgnore.add("file");
}

public DartJaguarClientCodegen() {
super();
browserClient = false;
Expand Down Expand Up @@ -110,20 +118,15 @@ public void processOpts() {

final String libFolder = sourceFolder + File.separator + "lib";
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml"));
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", ".analysis_options"));
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));

supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

final String authFolder = sourceFolder + File.separator + "lib" + File.separator + "auth";
supportingFiles.add(new SupportingFile("auth/api_key_auth.mustache", authFolder, "api_key_auth.dart"));
supportingFiles.add(new SupportingFile("auth/basic_auth.mustache", authFolder, "basic_auth.dart"));
supportingFiles.add(new SupportingFile("auth/oauth.mustache", authFolder, "oauth.dart"));
supportingFiles.add(new SupportingFile("auth/auth.mustache", authFolder, "auth.dart"));
}


@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
objs = super.postProcessModels(objs);
Expand All @@ -133,13 +136,14 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
Set<String> modelImports = new HashSet<>();
CodegenModel cm = (CodegenModel) mo.get("model");
for (String modelImport : cm.imports) {
if (!modelToIgnore.contains(modelImport.toLowerCase(Locale.ROOT))) {
if(!modelToIgnore.contains(modelImport.toLowerCase())) {
modelImports.add(underscore(modelImport));
}
}
cm.imports = modelImports;
cm.vendorExtensions.put("hasVars", cm.vars.size() > 0);
}
//objs.put("modelImports", modelImports);
return objs;
}

Expand All @@ -152,11 +156,11 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
Set<String> modelImports = new HashSet<>();

for (CodegenOperation op : operationList) {
op.httpMethod = StringUtils.capitalize(op.httpMethod.toLowerCase(Locale.ROOT));
op.httpMethod = StringUtils.capitalize(op.httpMethod.toLowerCase());
boolean isJson = true; //default to JSON
boolean isForm = false;
boolean isMultipart = false;
if (op.consumes != null) {
if(op.consumes != null) {
for (Map<String, String> consume : op.consumes) {
if (consume.containsKey("mediaType")) {
String type = consume.get("mediaType");
Expand All @@ -174,7 +178,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {

Set<String> imports = new HashSet<>();
for (String item : op.imports) {
if (!modelToIgnore.contains(item.toLowerCase(Locale.ROOT))) {
if(!modelToIgnore.contains(item.toLowerCase())) {
imports.add(underscore(item));
}
}
Expand All @@ -191,7 +195,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
jaguarPath = jaguarPath + items[i];
}

if (i != items.length - 1) {
if (i != items.length -1) {
jaguarPath = jaguarPath + "/";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{{appDescription}}}
{{/appDescription}}

This Dart package is automatically generated by the [Open API Codegen](https://github.com/OpenAPITools/openapi-generator) project:
This Dart package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:

- API version: {{appVersion}}
{{#artifactVersion}}
Expand Down Expand Up @@ -82,8 +82,8 @@ import 'package:{{pubName}}/api.dart';
{{/authMethods}}
{{/hasAuthMethods}}

final jaguarApiGen = JaguarApiGen();
var api_instance = jaguarApiGen.get{{classname}}();
final swaggerGen = SwaggerGen();
var api_instance = swaggerGen.get{{classname}}();
{{#allParams}}
var {{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}new {{dataType}}(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; // {{{dataType}}} | {{{description}}}
{{/allParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class {{classname}} extends _${{classname}}Client implements ApiClient {
/// {{summary}}
///
/// {{notes}}
@{{httpMethod}}Req(path: "{{path}}"{{#hasAuthMethods}}, metadata: {"auth": [{{#authMethods}} {"type": "{{type}}", "name": "{{name}}"{{#isApiKey}}, "keyName": "{{keyParamName}}", "where": "{{#isKeyInQuery}}query{{/isKeyInQuery}}{{#isKeyInHeader}}header{{/isKeyInHeader}}"{{/isApiKey}} }{{#hasMore}}, {{/hasMore}}{{/authMethods}}]}{{/hasAuthMethods}})
@{{httpMethod}}Req(path: '{{path}}')
Future<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{nickname}}(
{{#pathParams}}
@PathParam("{{baseName}}") {{dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}
{{dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}
{{/pathParams}}
{{#headerParams}}
{{#-first}}{{#hasPathParams}},{{/hasPathParams}}{{/-first}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
library {{pubName}}.api;

import 'package:http/http.dart';
import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
import 'package:{{pubName}}/auth/api_key_auth.dart';
import 'package:{{pubName}}/auth/basic_auth.dart';
import 'package:{{pubName}}/auth/oauth.dart';

{{#apiInfo}}{{#apis}}import 'package:{{pubName}}/api/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
Expand All @@ -16,52 +12,19 @@ final jsonJaguarRepo = JsonRepo()
{{#models}}{{#model}}..add({{classname}}Serializer())
{{/model}}{{/models}};

final _defaultInterceptors = [OAuthInterceptor(), BasicAuthInterceptor(), ApiKeyAuthInterceptor()];
final baseSwaggerPath = "{{basePath}}";
final _baseRoute = Route(baseSwaggerPath);

class JaguarApiGen {
List<Interceptor> interceptors;
String basePath = "{{basePath}}";
Route _baseRoute;
/**
* Add custom global interceptors, put overrideInterceptors to true to set your interceptors only (auth interceptors will not be added)
*/
JaguarApiGen({List<Interceptor> interceptors, bool overrideInterceptors = false, String baseUrl}) {
_baseRoute = Route(baseUrl ?? basePath).withClient(globalClient ?? IOClient());
if(interceptors == null) {
this.interceptors = _defaultInterceptors;
}
else if(overrideInterceptors){
this.interceptors = interceptors;
}
else {
this.interceptors = List.from(_defaultInterceptors)..addAll(interceptors);
}

this.interceptors.forEach((interceptor) {
class SwaggerGen {
final List<Interceptor> interceptors;
SwaggerGen({this.interceptors = const []}) {
interceptors.forEach((interceptor) {
_baseRoute.before(interceptor.before);
_baseRoute.after(interceptor.after);
});
}

void setOAuthToken(String name, String token) {
(_defaultInterceptors[0] as OAuthInterceptor).tokens[name] = token;
}

void setBasicAuth(String name, String username, String password) {
(_defaultInterceptors[1] as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
}

void setApiKey(String name, String apiKey) {
(_defaultInterceptors[2] as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
}

{{#apiInfo}}{{#apis}}
/**
* Get {{classname}} instance, base route and serializer can be overridden by a given but be careful,
* by doing that all interceptors will not be executed
*/
{{classname}} get{{classname}}({Route base, SerializerRepo serializers}) {
{{#apiInfo}}{{#apis}}{{classname}} get{{classname}}({Route base, SerializerRepo serializers}) {
if(base == null) {
base = _baseRoute;
}
Expand All @@ -72,4 +35,4 @@ class JaguarApiGen {
}

{{/apis}}{{/apiInfo}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: {{pubDescription}}
environment:
sdk: ">=2.0.0 <3.0.0"
dependencies:
jaguar_retrofit: '^2.5.4'
jaguar_serializer: '^2.2.2'
jaguar_retrofit: '^2.4.1'
jaguar_serializer: '^2.2.0'
dev_dependencies:
jaguar_retrofit_gen: '^2.5.2'
jaguar_retrofit_gen: '^2.4.2'
jaguar_serializer_cli: '^2.2.1'
build_runner: '^0.10.0'
Loading

0 comments on commit 012247b

Please sign in to comment.