Skip to content
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 @@ -181,6 +181,15 @@ public class Generate extends OpenApiGeneratorCommand {
+ " You can also have multiple occurrences of this option.")
private List<String> schemaMappings = new ArrayList<>();

@Option(
name = {"--forced-generate-schemas"},
title = "forced generate schemas",
description = "comma-separated list of schema names that must be generated even when listed "
+ "in schemaMappings or importMappings. Example: MyEnum,OtherSchema."
+ " Use the wildcard '*' to force-generate all mapped schemas at once."
+ " You can also have multiple occurrences of this option.")
private List<String> forcedGenerateSchemas = new ArrayList<>();

@Option(
name = {"--inline-schema-name-mappings"},
title = "inline schema name mappings",
Expand Down Expand Up @@ -508,6 +517,7 @@ public void execute() {
applyInstantiationTypesKvpList(instantiationTypes, configurator);
applyImportMappingsKvpList(importMappings, configurator);
applySchemaMappingsKvpList(schemaMappings, configurator);
applyForcedGenerateSchemasKvpList(forcedGenerateSchemas, configurator);
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
applyInlineSchemaOptionsKvpList(inlineSchemaOptions, configurator);
applyNameMappingsKvpList(nameMappings, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class GeneratorSettings implements Serializable {
private final Map<String, Object> additionalProperties;
private final Map<String, String> importMappings;
private final Map<String, String> schemaMappings;
private final Set<String> forcedGenerateSchemas;
private final Map<String, String> inlineSchemaNameMappings;
private final Map<String, String> inlineSchemaOptions;
private final Map<String, String> nameMappings;
Expand Down Expand Up @@ -253,6 +254,16 @@ public Map<String, String> getSchemaMappings() {
return schemaMappings;
}

/**
* Gets the set of schema names that must be generated even when listed in schemaMappings or importMappings.
* Use {@code "*"} as a wildcard to force-generate all mapped schemas at once.
*
* @return the forced generate schemas
*/
public Set<String> getForcedGenerateSchemas() {
return forcedGenerateSchemas;
}

/**
* Gets inline schema name mappings between an inline schema name and the new name.
*
Expand Down Expand Up @@ -450,6 +461,7 @@ private GeneratorSettings(Builder builder) {
typeMappings = Collections.unmodifiableMap(builder.typeMappings);
importMappings = Collections.unmodifiableMap(builder.importMappings);
schemaMappings = Collections.unmodifiableMap(builder.schemaMappings);
forcedGenerateSchemas = Collections.unmodifiableSet(builder.forcedGenerateSchemas);
inlineSchemaNameMappings = Collections.unmodifiableMap(builder.inlineSchemaNameMappings);
inlineSchemaOptions = Collections.unmodifiableMap(builder.inlineSchemaOptions);
nameMappings = Collections.unmodifiableMap(builder.nameMappings);
Expand Down Expand Up @@ -530,6 +542,7 @@ public GeneratorSettings() {
additionalProperties = Collections.unmodifiableMap(new HashMap<>(0));
importMappings = Collections.unmodifiableMap(new HashMap<>(0));
schemaMappings = Collections.unmodifiableMap(new HashMap<>(0));
forcedGenerateSchemas = Collections.unmodifiableSet(new HashSet<>(0));
inlineSchemaNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
inlineSchemaOptions = Collections.unmodifiableMap(new HashMap<>(0));
nameMappings = Collections.unmodifiableMap(new HashMap<>(0));
Expand Down Expand Up @@ -593,6 +606,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
if (copy.getSchemaMappings() != null) {
builder.schemaMappings.putAll(copy.getSchemaMappings());
}
if (copy.getForcedGenerateSchemas() != null) {
builder.forcedGenerateSchemas.addAll(copy.getForcedGenerateSchemas());
}
if (copy.getInlineSchemaNameMappings() != null) {
builder.inlineSchemaNameMappings.putAll(copy.getInlineSchemaNameMappings());
}
Expand Down Expand Up @@ -660,6 +676,7 @@ public static final class Builder {
private Map<String, Object> additionalProperties;
private Map<String, String> importMappings;
private Map<String, String> schemaMappings;
private Set<String> forcedGenerateSchemas;
private Map<String, String> inlineSchemaNameMappings;
private Map<String, String> inlineSchemaOptions;
private Map<String, String> nameMappings;
Expand Down Expand Up @@ -687,6 +704,7 @@ public Builder() {
additionalProperties = new HashMap<>();
importMappings = new HashMap<>();
schemaMappings = new HashMap<>();
forcedGenerateSchemas = new HashSet<>();
inlineSchemaNameMappings = new HashMap<>();
inlineSchemaOptions = new HashMap<>();
nameMappings = new HashMap<>();
Expand Down Expand Up @@ -938,6 +956,35 @@ public Builder withSchemaMapping(String key, String value) {
return this;
}

/**
* Sets the {@code forcedGenerateSchemas} (schemas to generate even when listed in schemaMappings or importMappings).
* Use {@code "*"} as a wildcard to force-generate all mapped schemas at once.
* and returns a reference to this Builder so that the methods can be chained together.
*
* @param schemas the {@code forcedGenerateSchemas} to set
* @return a reference to this Builder
*/
public Builder withForcedGenerateSchemas(Set<String> schemas) {
this.forcedGenerateSchemas = schemas;
return this;
}

/**
* Adds a single schema name to {@code forcedGenerateSchemas} (schemas to generate even when listed in schemaMappings or importMappings).
* Use {@code "*"} as a wildcard to force-generate all mapped schemas at once.
* Returns a reference to this Builder so that the methods can be chained together.
*
* @param schema the schema name to add
* @return a reference to this Builder
*/
public Builder withForcedGenerateSchema(String schema) {
if (this.forcedGenerateSchemas == null) {
this.forcedGenerateSchemas = new HashSet<>();
}
this.forcedGenerateSchemas.add(schema);
return this;
}

/**
* Sets the {@code importMappings} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -1350,6 +1397,7 @@ public String toString() {
", typeMappings=" + typeMappings +
", additionalProperties=" + additionalProperties +
", importMappings=" + importMappings +
", forcedGenerateSchemas=" + forcedGenerateSchemas +
", languageSpecificPrimitives=" + languageSpecificPrimitives +
", openapiGeneratorIgnoreList=" + openapiGeneratorIgnoreList +
", reservedWordsMappings=" + reservedWordsMappings +
Expand Down Expand Up @@ -1383,6 +1431,7 @@ public boolean equals(Object o) {
Objects.equals(getAdditionalProperties(), that.getAdditionalProperties()) &&
Objects.equals(getImportMappings(), that.getImportMappings()) &&
Objects.equals(getSchemaMappings(), that.getSchemaMappings()) &&
Objects.equals(getForcedGenerateSchemas(), that.getForcedGenerateSchemas()) &&
Objects.equals(getInlineSchemaNameMappings(), that.getInlineSchemaNameMappings()) &&
Objects.equals(getInlineSchemaOptions(), that.getInlineSchemaOptions()) &&
Objects.equals(getNameMappings(), that.getNameMappings()) &&
Expand Down Expand Up @@ -1421,6 +1470,7 @@ public int hashCode() {
getAdditionalProperties(),
getImportMappings(),
getSchemaMappings(),
getForcedGenerateSchemas(),
getInlineSchemaNameMappings(),
getInlineSchemaOptions(),
getNameMappings(),
Expand Down
5 changes: 5 additions & 0 deletions modules/openapi-generator-gradle-plugin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ apply plugin: 'org.openapi.generator'
|None
|specifies mappings between the schema and the new name in the format of schema_a=Cat,schema_b=Bird. https://openapi-generator.tech/docs/customization/#schema-mapping

|forcedGenerateSchemas
|List / Provider<List>
|None
|Forces generation of the named schemas even when they are listed in `schemaMappings` or `importMappings` (which would normally suppress their generation). The primary use case is producing a *reference copy* of a hand-written class — for example, a custom enum that replaces a generated one — so you can assert in a test that the hand-written version has not diverged from what the generator would produce. List of schema names, e.g. `["MyEnum", "OtherSchema"]`. Use `["*"]` as a wildcard to force-generate *all* schemas that would otherwise be suppressed by a mapping.

|nameMappings
|Map / Provider<Map>
|None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
openapiGeneratorIgnoreList.set(generate.openapiGeneratorIgnoreList)
importMappings.set(generate.importMappings)
schemaMappings.set(generate.schemaMappings)
forcedGenerateSchemas.set(generate.forcedGenerateSchemas)
inlineSchemaNameMappings.set(generate.inlineSchemaNameMappings)
inlineSchemaOptions.set(generate.inlineSchemaOptions)
nameMappings.set(generate.nameMappings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ open class OpenApiGeneratorGenerateExtension(private val project: Project) {
*/
val schemaMappings = project.objects.mapProperty<String, String>()

/**
* Specifies schema names that must be generated even when listed in schemaMappings or importMappings
*/
val forcedGenerateSchemas = project.objects.listProperty<String>()

/**
* Specifies mappings between an inline schema name and the new name
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ interface OpenApiWorkParameters : WorkParameters {
val instantiationTypes: MapProperty<String, String>
val importMappings: MapProperty<String, String>
val schemaMappings: MapProperty<String, String>
val forcedGenerateSchemas: ListProperty<String>
val inlineSchemaNameMappings: MapProperty<String, String>
val inlineSchemaOptions: MapProperty<String, String>
val nameMappings: MapProperty<String, String>
Expand Down Expand Up @@ -204,6 +205,7 @@ abstract class OpenApiWorkAction : WorkAction<OpenApiWorkParameters> {
params.instantiationTypes.orNull?.forEach { (k, v) -> configurator.addInstantiationType(k, v) }
params.importMappings.orNull?.forEach { (k, v) -> configurator.addImportMapping(k, v) }
params.schemaMappings.orNull?.forEach { (k, v) -> configurator.addSchemaMapping(k, v) }
params.forcedGenerateSchemas.orNull?.forEach { configurator.addForcedGenerateSchema(it) }
params.inlineSchemaNameMappings.orNull?.forEach { (k, v) -> configurator.addInlineSchemaNameMapping(k, v) }
params.inlineSchemaOptions.orNull?.forEach { (k, v) -> configurator.addInlineSchemaOption(k, v) }
params.nameMappings.orNull?.forEach { (k, v) -> configurator.addNameMapping(k, v) }
Expand Down Expand Up @@ -549,6 +551,13 @@ abstract class GenerateTask : DefaultTask() {
@get:Input
abstract val schemaMappings: MapProperty<String, String>

/**
* Specifies schema names that must be generated even when listed in schemaMappings or importMappings.
*/
@get:Optional
@get:Input
abstract val forcedGenerateSchemas: ListProperty<String>

/**
* Specifies mappings between the inline scheme name and the new name
*/
Expand Down Expand Up @@ -964,6 +973,7 @@ abstract class GenerateTask : DefaultTask() {
parameters.instantiationTypes.set(instantiationTypes)
parameters.importMappings.set(importMappings)
parameters.schemaMappings.set(schemaMappings)
parameters.forcedGenerateSchemas.set(forcedGenerateSchemas)
parameters.inlineSchemaNameMappings.set(inlineSchemaNameMappings)
parameters.inlineSchemaOptions.set(inlineSchemaOptions)
parameters.nameMappings.set(nameMappings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "schemaMappings", property = "openapi.generator.maven.plugin.schemaMappings")
private List<String> schemaMappings;

/**
* A list of schema names that must be generated even when listed in schemaMappings or importMappings.
* Use {@code <param>*</param>} as a wildcard to force-generate all mapped schemas at once.
*/
@Parameter(name = "forcedGenerateSchemas", property = "openapi.generator.maven.plugin.forcedGenerateSchemas")
private List<String> forcedGenerateSchemas;

/**
* A map of inline scheme names and the new names
*/
Expand Down Expand Up @@ -824,6 +831,13 @@ public void execute() throws MojoExecutionException {
configurator);
}

// Retained for backwards-compatibility with configOptions -> forced-generate-schemas
if (forcedGenerateSchemas == null && configOptions.containsKey("forced-generate-schemas")) {
applyForcedGenerateSchemasKvpList(
Arrays.asList(configOptions.get("forced-generate-schemas").toString().split(",")),
configurator);
}

// Retained for backwards-compatibility with configOptions -> inline-schema-name-mappings
if (inlineSchemaNameMappings == null && configOptions.containsKey("inline-schema-name-mappings")) {
applyInlineSchemaNameMappingsKvp(configOptions.get("inline-schema-name-mappings").toString(),
Expand Down Expand Up @@ -891,6 +905,11 @@ public void execute() throws MojoExecutionException {
applySchemaMappingsKvpList(schemaMappings, configurator);
}

// Apply Forced Generate Schemas
if (forcedGenerateSchemas != null && (configOptions == null || !configOptions.containsKey("forced-generate-schemas"))) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
applyForcedGenerateSchemasKvpList(forcedGenerateSchemas, configurator);
}

// Apply Inline Schema Name Mappings
if (inlineSchemaNameMappings != null && (configOptions == null || !configOptions.containsKey("inline-schema-name-mappings"))) {
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ public interface CodegenConfig {

Map<String, String> schemaMapping();

/**
* Returns the set of schema names that must be generated even when they appear in
* schemaMappings or importMappings (which would normally suppress their generation).
* <p>
* Use {@link CodegenConstants#FORCE_GENERATE_ALL_SCHEMAS} ({@code "*"}) as a wildcard
* to force-generate <em>all</em> mapped schemas at once.
*/
Set<String> forcedGenerateSchemas();

Map<String, String> inlineSchemaNameMapping();

Map<String, String> inlineSchemaOption();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class CodegenConstants {
public static final String SKIP_FORM_MODEL = "skipFormModel";
/* /end System Properties */

/**
* Wildcard token for {@code forcedGenerateSchemas}: when this value is present in the set,
* all schemas are generated even if they appear in schemaMappings or importMappings.
*/
public static final String FORCE_GENERATE_ALL_SCHEMAS = "*";

public static final String API_NAME = "apiName";

public static final String API_PACKAGE = "apiPackage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public class DefaultCodegen implements CodegenConfig {
protected Map<String, String> importMapping = new HashMap<>();
// a map to store the mapping between a schema and the new one
protected Map<String, String> schemaMapping = new HashMap<>();
// a set of schema names that must be generated even when listed in schemaMappings or importMappings.
// Use CodegenConstants.FORCE_GENERATE_ALL_SCHEMAS ("*") to force-generate all mapped schemas.
protected Set<String> forcedGenerateSchemas = new HashSet<>();
// a map to store the mapping between inline schema and the name provided by the user
protected Map<String, String> inlineSchemaNameMapping = new HashMap<>();
// a map to store the inline schema naming conventions
Expand Down Expand Up @@ -1302,6 +1305,11 @@ public Map<String, String> schemaMapping() {
return schemaMapping;
}

@Override
public Set<String> forcedGenerateSchemas() {
return forcedGenerateSchemas;
}

@Override
public Map<String, String> inlineSchemaNameMapping() {
return inlineSchemaNameMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,17 @@ private void generateModelTests(List<File> files, Map<String, Object> models, St
}
}

/**
* Returns {@code true} if the named schema should be generated even when it appears in
* schemaMappings or importMappings. This is the case when the schema name is explicitly
* listed in {@code forcedGenerateSchemas} or when the wildcard
* {@link CodegenConstants#FORCE_GENERATE_ALL_SCHEMAS} ({@code "*"}) is present.
*/
private boolean isNotForcedGenerate(String schemaName) {
return !config.forcedGenerateSchemas().contains(CodegenConstants.FORCE_GENERATE_ALL_SCHEMAS)
&& !config.forcedGenerateSchemas().contains(schemaName);
}

private void generateModelDocumentation(List<File> files, Map<String, Object> models, String modelName) throws IOException {
for (String templateName : config.modelDocTemplateFiles().keySet()) {
String docExtension = config.getDocExtension();
Expand Down Expand Up @@ -467,8 +478,8 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
for (String name : modelKeys) {
processedModels.add(name);
try {
//don't generate models that have an import mapping
if (config.schemaMapping().containsKey(name)) {
//don't generate models that have an import mapping or are in the list of schemas to always generate
if (config.schemaMapping().containsKey(name) && isNotForcedGenerate(name)) {
LOGGER.info("Model {} not generated due to schema mapping", name);
continue;
}
Expand Down Expand Up @@ -549,8 +560,8 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
ModelsMap models = allProcessedModels.get(modelName);
models.put("modelPackage", config.modelPackage());
try {
//don't generate models that have a schema mapping
if (config.schemaMapping().containsKey(modelName)) {
//don't generate models that have a schema mapping or are in the list of schemas to always generate
if (config.schemaMapping().containsKey(modelName) && isNotForcedGenerate(modelName)) {
continue;
}

Expand Down
Loading
Loading