Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'x-generate-alias-as-model' extension to allow enabling generating alias as model per-schema #6937

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5906,7 +5906,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 @@ -5947,7 +5947,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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