mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-18 10:09:10 +00:00
Dart (Jaguar) client generator (#998)
* generator for dart jaguar
* migrate to openapi-generator
* fix locale issue
* add auth generation for dart jaguar
* fix generation of jaguar api with auth (#1009)
* update deps (#1016)
* add CI tests for dart jaguar
* update dart jaguar bin script
* trigger build failure
* Revert "trigger build failure"
This reverts commit a7d8bfd47a.
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
import io.swagger.v3.oas.models.media.*;
|
||||
|
||||
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("list");
|
||||
modelToIgnore.add("map");
|
||||
modelToIgnore.add("file");
|
||||
}
|
||||
public DartJaguarClientCodegen() {
|
||||
super();
|
||||
browserClient = false;
|
||||
outputFolder = "generated-code/dart-jaguar";
|
||||
embeddedTemplateDir = templateDir = "dart-jaguar";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "dart-jaguar";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Generates a Dart Jaguar client library.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toDefaultValue(Schema p) {
|
||||
if (ModelUtils.isMapSchema(p)) {
|
||||
return "const {}";
|
||||
} else if (ModelUtils.isArraySchema(p)) {
|
||||
return "const []";
|
||||
}
|
||||
return super.toDefaultValue(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
if (additionalProperties.containsKey(PUB_NAME)) {
|
||||
this.setPubName((String) additionalProperties.get(PUB_NAME));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put(PUB_NAME, pubName);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(PUB_VERSION)) {
|
||||
this.setPubVersion((String) additionalProperties.get(PUB_VERSION));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put(PUB_VERSION, pubVersion);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(PUB_DESCRIPTION)) {
|
||||
this.setPubDescription((String) additionalProperties.get(PUB_DESCRIPTION));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put(PUB_DESCRIPTION, pubDescription);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(USE_ENUM_EXTENSION)) {
|
||||
this.setUseEnumExtension(convertPropertyToBooleanAndWriteBack(USE_ENUM_EXTENSION));
|
||||
} else {
|
||||
// Not set, use to be passed to template.
|
||||
additionalProperties.put(USE_ENUM_EXTENSION, useEnumExtension);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
|
||||
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
|
||||
}
|
||||
|
||||
// make api and model doc path available in mustache template
|
||||
additionalProperties.put("apiDocPath", apiDocPath);
|
||||
additionalProperties.put("modelDocPath", modelDocPath);
|
||||
|
||||
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("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);
|
||||
List<Object> models = (List<Object>) objs.get("models");
|
||||
for (Object _mo : models) {
|
||||
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||
Set<String> modelImports = new HashSet<>();
|
||||
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||
for (String modelImport : cm.imports) {
|
||||
if(!modelToIgnore.contains(modelImport.toLowerCase(Locale.ROOT))) {
|
||||
modelImports.add(underscore(modelImport));
|
||||
}
|
||||
}
|
||||
cm.imports = modelImports;
|
||||
cm.vendorExtensions.put("hasVars", cm.vars.size() > 0);
|
||||
}
|
||||
//objs.put("modelImports", modelImports);
|
||||
return objs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||
objs = super.postProcessOperations(objs);
|
||||
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
||||
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
|
||||
|
||||
Set<String> modelImports = new HashSet<>();
|
||||
|
||||
for (CodegenOperation op : operationList) {
|
||||
op.httpMethod = StringUtils.capitalize(op.httpMethod.toLowerCase(Locale.ROOT));
|
||||
boolean isJson = true; //default to JSON
|
||||
boolean isForm = false;
|
||||
boolean isMultipart = false;
|
||||
if(op.consumes != null) {
|
||||
for (Map<String, String> consume : op.consumes) {
|
||||
if (consume.containsKey("mediaType")) {
|
||||
String type = consume.get("mediaType");
|
||||
isJson = type.equalsIgnoreCase("application/json");
|
||||
isForm = type.equalsIgnoreCase("application/x-www-form-urlencoded");
|
||||
isMultipart = type.equalsIgnoreCase("multipart/form-data");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
op.vendorExtensions.put("isJson", isJson);
|
||||
op.vendorExtensions.put("isForm", isForm);
|
||||
op.vendorExtensions.put("isMultipart", isMultipart);
|
||||
|
||||
Set<String> imports = new HashSet<>();
|
||||
for (String item : op.imports) {
|
||||
if(!modelToIgnore.contains(item.toLowerCase(Locale.ROOT))) {
|
||||
imports.add(underscore(item));
|
||||
}
|
||||
}
|
||||
modelImports.addAll(imports);
|
||||
op.imports = imports;
|
||||
|
||||
String[] items = op.path.split("/", -1);
|
||||
String jaguarPath = "";
|
||||
|
||||
for (int i = 0; i < items.length; ++i) {
|
||||
if (items[i].matches("^\\{(.*)\\}$")) { // wrap in {}
|
||||
jaguarPath = jaguarPath + ":" + items[i].replace("{", "").replace("}", "");
|
||||
} else {
|
||||
jaguarPath = jaguarPath + items[i];
|
||||
}
|
||||
|
||||
if (i != items.length -1) {
|
||||
jaguarPath = jaguarPath + "/";
|
||||
}
|
||||
}
|
||||
|
||||
op.path = jaguarPath;
|
||||
}
|
||||
|
||||
objs.put("modelImports", modelImports);
|
||||
|
||||
return objs;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user