Skip to content

Commit

Permalink
Add 'x-generate-alias-as-model' extension to allow enabling generatin…
Browse files Browse the repository at this point in the history
…g alias as model per-schema (#6937)
  • Loading branch information
bkabrda committed Jul 23, 2020
1 parent d1fe2e1 commit 3199ddc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
Expand Up @@ -5909,7 +5909,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S

if (ModelUtils.isMapSchema(schema)) {
// Schema with additionalproperties: true (including composed schemas with additionalproperties: true)
if (ModelUtils.isGenerateAliasAsModel() && StringUtils.isNotBlank(name)) {
if (ModelUtils.isGenerateAliasAsModel(schema) && StringUtils.isNotBlank(name)) {
this.addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, true);
} else {
Schema inner = getAdditionalProperties(schema);
Expand Down Expand Up @@ -5950,7 +5950,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
setParameterNullable(codegenParameter, codegenProperty);
}
} else if (ModelUtils.isArraySchema(schema)) {
if (ModelUtils.isGenerateAliasAsModel() && StringUtils.isNotBlank(name)) {
if (ModelUtils.isGenerateAliasAsModel(schema) && StringUtils.isNotBlank(name)) {
this.addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, true);
} else {
final ArraySchema arraySchema = (ArraySchema) schema;
Expand Down
Expand Up @@ -452,13 +452,13 @@ void generateModels(List<File> files, List<Object> allModels, List<String> unuse
// A composed schema (allOf, oneOf, anyOf) is considered a Map schema if the additionalproperties attribute is set
// for that composed schema. However, in the case of a composed schema, the properties are defined or referenced
// in the inner schemas, and the outer schema does not have properties.
if (!ModelUtils.isGenerateAliasAsModel() && !ModelUtils.isComposedSchema(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
if (!ModelUtils.isGenerateAliasAsModel(schema) && !ModelUtils.isComposedSchema(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
// schema without property, i.e. alias to map
LOGGER.info("Model {} not generated since it's an alias to map (without property) and `generateAliasAsModel` is set to false (default)", name);
continue;
}
} else if (ModelUtils.isArraySchema(schema)) { // check to see if it's an "array" model
if (!ModelUtils.isGenerateAliasAsModel() && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
if (!ModelUtils.isGenerateAliasAsModel(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
// schema without property, i.e. alias to array
LOGGER.info("Model {} not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)", name);
continue;
Expand Down
Expand Up @@ -1400,7 +1400,7 @@ public void setTestDataFile(File testDataFile) {

@Override
public String toDefaultValue(Schema p) {
if (ModelUtils.isGenerateAliasAsModel() && StringUtils.isNotEmpty(p.get$ref())) {
if (ModelUtils.isGenerateAliasAsModel(p) && StringUtils.isNotEmpty(p.get$ref())) {
Schema<?> ref = ModelUtils.getReferencedSchema(this.openAPI, p);
if (ModelUtils.isArraySchema(ref) || ModelUtils.isMapSchema(ref)) {
String typeDeclaration = getTypeDeclaration(p);
Expand Down
Expand Up @@ -88,6 +88,10 @@ public static boolean isGenerateAliasAsModel() {
return Boolean.parseBoolean(GlobalSettings.getProperty(generateAliasAsModelKey, "false"));
}

public static boolean isGenerateAliasAsModel(Schema schema) {
return isGenerateAliasAsModel() || (schema.getExtensions() != null && schema.getExtensions().getOrDefault("x-generate-alias-as-model", false).equals(true));
}

/**
* Searches for the model by name in the map of models and returns it
*
Expand Down Expand Up @@ -1037,7 +1041,7 @@ public static Schema unaliasSchema(OpenAPI openAPI,
// top-level enum class
return schema;
} else if (isArraySchema(ref)) {
if (isGenerateAliasAsModel()) {
if (isGenerateAliasAsModel(ref)) {
return schema; // generate a model extending array
} else {
return unaliasSchema(openAPI, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())),
Expand All @@ -1049,7 +1053,7 @@ public static Schema unaliasSchema(OpenAPI openAPI,
if (ref.getProperties() != null && !ref.getProperties().isEmpty()) // has at least one property
return schema; // treat it as model
else {
if (isGenerateAliasAsModel()) {
if (isGenerateAliasAsModel(ref)) {
return schema; // generate a model extending map
} else {
// treat it as a typical map
Expand Down

0 comments on commit 3199ddc

Please sign in to comment.