forked from loafle/openapi-generator-original
Add 'x-generate-alias-as-model' extension to allow enabling generating alias as model per-schema (#6937)
This commit is contained in:
@@ -5909,7 +5909,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
|
||||
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);
|
||||
@@ -5950,7 +5950,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
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;
|
||||
|
||||
@@ -452,13 +452,13 @@ public class DefaultGenerator implements Generator {
|
||||
// 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;
|
||||
|
||||
@@ -1400,7 +1400,7 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
|
||||
@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);
|
||||
|
||||
@@ -88,6 +88,10 @@ public class ModelUtils {
|
||||
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
|
||||
*
|
||||
@@ -1037,7 +1041,7 @@ public class ModelUtils {
|
||||
// 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())),
|
||||
@@ -1049,7 +1053,7 @@ public class ModelUtils {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user