fix generateAliasAsModels in default generator (#3951)

This commit is contained in:
William Cheng 2019-09-26 11:31:25 +08:00 committed by GitHub
parent e20af77944
commit bd992a4218
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 20 deletions

View File

@ -35,6 +35,7 @@ import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.config.GlobalSettings; import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.api.TemplatingEngineAdapter; import org.openapitools.codegen.api.TemplatingEngineAdapter;
import org.openapitools.codegen.ignore.CodegenIgnoreProcessor; import org.openapitools.codegen.ignore.CodegenIgnoreProcessor;
import org.openapitools.codegen.languages.PythonClientExperimentalCodegen;
import org.openapitools.codegen.meta.GeneratorMetadata; import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability; import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.serializer.SerializerUtils; import org.openapitools.codegen.serializer.SerializerUtils;
@ -492,7 +493,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
Map<String, Object> modelTemplate = (Map<String, Object>) ((List<Object>) models.get("models")).get(0); Map<String, Object> modelTemplate = (Map<String, Object>) ((List<Object>) models.get("models")).get(0);
if (modelTemplate != null && modelTemplate.containsKey("model")) { if (modelTemplate != null && modelTemplate.containsKey("model")) {
CodegenModel m = (CodegenModel) modelTemplate.get("model"); CodegenModel m = (CodegenModel) modelTemplate.get("model");
if (m.isAlias && !ModelUtils.isGenerateAliasAsModel()) { if (m.isAlias && !(config instanceof PythonClientExperimentalCodegen)) {
// alias to number, string, enum, etc, which should not be generated as model
// for PythonClientExperimentalCodegen, all aliases are generated as models
continue; // Don't create user-defined classes for aliases continue; // Don't create user-defined classes for aliases
} }
} }
@ -942,7 +945,6 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
* Returns the path of a template, allowing access to the template where consuming literal contents aren't desirable or possible. * Returns the path of a template, allowing access to the template where consuming literal contents aren't desirable or possible.
* *
* @param name the template name (e.g. model.mustache) * @param name the template name (e.g. model.mustache)
*
* @return The {@link Path} to the template * @return The {@link Path} to the template
*/ */
@Override @Override
@ -1062,21 +1064,21 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
if (authMethods != null && !authMethods.isEmpty()) { if (authMethods != null && !authMethods.isEmpty()) {
codegenOperation.authMethods = config.fromSecurity(authMethods); codegenOperation.authMethods = config.fromSecurity(authMethods);
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
if (codegenOperation.authMethods != null){ if (codegenOperation.authMethods != null) {
for (CodegenSecurity security : codegenOperation.authMethods){ for (CodegenSecurity security : codegenOperation.authMethods) {
if (security != null && security.isBasicBearer != null && security.isBasicBearer && if (security != null && security.isBasicBearer != null && security.isBasicBearer &&
securities != null){ securities != null) {
for (SecurityRequirement req : securities){ for (SecurityRequirement req : securities) {
if (req == null) continue; if (req == null) continue;
for (String key : req.keySet()){ for (String key : req.keySet()) {
if (security.name != null && key.equals(security.name)){ if (security.name != null && key.equals(security.name)) {
int count = 0; int count = 0;
for (String sc : req.get(key)){ for (String sc : req.get(key)) {
Map<String, Object> scope = new HashMap<String, Object>(); Map<String, Object> scope = new HashMap<String, Object>();
scope.put("scope", sc); scope.put("scope", sc);
scope.put("description", ""); scope.put("description", "");
count++; count++;
if (req.get(key) != null && count < req.get(key).size()){ if (req.get(key) != null && count < req.get(key).size()) {
scope.put("hasMore", "true"); scope.put("hasMore", "true");
} else { } else {
scope.put("hasMore", null); scope.put("hasMore", null);

View File

@ -78,10 +78,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
// default this to true so the python ModelSimple models will be generated // default this to true so the python ModelSimple models will be generated
ModelUtils.setGenerateAliasAsModel(true); ModelUtils.setGenerateAliasAsModel(true);
if (additionalProperties.containsKey(CodegenConstants.GENERATE_ALIAS_AS_MODEL)) { LOGGER.info(CodegenConstants.GENERATE_ALIAS_AS_MODEL + " is hard coded to true in this generator. Alias models will only be generated if they contain validations or enums");
LOGGER.info(CodegenConstants.GENERATE_ALIAS_AS_MODEL + " is hard coded to true in this generator. Alias models will only be generated if they contain validations or enums");
}
} }
/** /**
@ -108,6 +105,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
/** /**
* Return the default value of the property * Return the default value of the property
*
* @param p OpenAPI property object * @param p OpenAPI property object
* @return string presentation of the default value of the property * @return string presentation of the default value of the property
*/ */
@ -194,7 +192,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
} }
return defaultValue; return defaultValue;
} else { } else {
return defaultObject.toString(); return defaultObject.toString();
} }
} }
@ -387,7 +385,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
} else { } else {
if (cp.isEnum == true || cp.hasValidation == true) { if (cp.isEnum == true || cp.hasValidation == true) {
// this model has validations and/or enums so we will generate it // this model has validations and/or enums so we will generate it
Schema sc = ModelUtils.getSchemaFromResponse(response); Schema sc = ModelUtils.getSchemaFromResponse(response);
newBaseType = ModelUtils.getSimpleRef(sc.get$ref()); newBaseType = ModelUtils.getSimpleRef(sc.get$ref());
} }
} }
@ -406,9 +404,9 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
/** /**
* Set op's returnBaseType, returnType, examples etc. * Set op's returnBaseType, returnType, examples etc.
* *
* @param operation endpoint Operation * @param operation endpoint Operation
* @param schemas a map of the schemas in the openapi spec * @param schemas a map of the schemas in the openapi spec
* @param op endpoint CodegenOperation * @param op endpoint CodegenOperation
* @param methodResponse the default ApiResponse for the endpoint * @param methodResponse the default ApiResponse for the endpoint
*/ */
@Override @Override
@ -568,7 +566,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
// fix all property references to non-object models, make those properties non-primitive and // fix all property references to non-object models, make those properties non-primitive and
// set their dataType and complexType to the model name, so documentation will refer to the correct model // set their dataType and complexType to the model name, so documentation will refer to the correct model
ArrayList<List<CodegenProperty>> listOfLists= new ArrayList<List<CodegenProperty>>(); ArrayList<List<CodegenProperty>> listOfLists = new ArrayList<List<CodegenProperty>>();
listOfLists.add(result.vars); listOfLists.add(result.vars);
listOfLists.add(result.allVars); listOfLists.add(result.allVars);
listOfLists.add(result.requiredVars); listOfLists.add(result.requiredVars);