Skip to content
Draft
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
4 changes: 4 additions & 0 deletions bin/configs/kotlin-spring-boot-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ outputDir: samples/server/petstore/kotlin-springboot-3
library: spring-boot
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-spring
schemaMappings:
Category: com.example.mapped.Category
forcedGenerateSchemas:
- Category
additionalProperties:
generateJsonIncludeAnnotations: "true"
generateJsonSetterNullsAnnotations: "true"
Expand Down
4 changes: 4 additions & 0 deletions bin/configs/spring-boot-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ generatorName: spring
outputDir: samples/openapi3/server/petstore/springboot-3
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
schemaMappings:
Category: com.example.mapped.Category
forcedGenerateSchemas:
- Category
additionalProperties:
generateJsonIncludeAnnotations: "true"
generateJsonSetterNullsAnnotations: "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ public class Generate extends OpenApiGeneratorCommand {
@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."
description = "comma-separated list of mapping-suppressed schemas to emit as isolated shadow models."
+ " Example: MyEnum,OtherSchema."
+ " Use the wildcard '*' to include all mapping-suppressed schemas."
+ " Supported families: Java, Groovy, Kotlin, C#, Python, Python Pydantic v1, PHP,"
+ " Go client, Perl, PowerShell, R, and Ruby; others fail before writing files."
+ " You can also have multiple occurrences of this option.")
private List<String> forcedGenerateSchemas = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ public Map<String, String> getSchemaMappings() {
}

/**
* 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.
* Gets the mapping-suppressed schemas to emit as isolated shadow models.
* Use {@code "*"} as a wildcard to include all mapping-suppressed schemas.
* Supported families are Java, Groovy, Kotlin, C#, Python, Python Pydantic v1, PHP, Go client, Perl,
* PowerShell, R, and Ruby; other generators fail before writing files.
*
* @return the forced generate schemas
*/
Expand Down Expand Up @@ -974,9 +976,10 @@ public Builder withSchemaMapping(String key, String value) {
}

/**
* 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.
* Sets the mapping-suppressed schemas to emit as isolated shadow models.
* Use {@code "*"} as a wildcard to include all mapping-suppressed schemas.
* Unsupported generator families fail before writing files.
* 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
Expand All @@ -987,8 +990,9 @@ public Builder withForcedGenerateSchemas(Set<String> schemas) {
}

/**
* 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.
* Adds a mapping-suppressed schema to emit as an isolated shadow model.
* Use {@code "*"} as a wildcard to include all mapping-suppressed schemas.
* Unsupported generator families fail before writing files.
* Returns a reference to this Builder so that the methods can be chained together.
*
* @param schema the schema name to add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ 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
* Specifies mapping-suppressed schemas to emit as isolated shadow models.
*
* Use `"*"` to include all mapping-suppressed schemas. Unmapped schemas remain in normal
* generation, and generated APIs and supporting-file metadata continue to use mapped classes.
* Supported families are Java, Groovy, Kotlin, C#, Python, Python Pydantic v1, PHP, Go client, Perl,
* PowerShell, R, and Ruby; other generators fail before writing files.
*/
val forcedGenerateSchemas = project.objects.listProperty<String>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,12 @@ abstract class GenerateTask : DefaultTask() {
abstract val schemaMappings: MapProperty<String, String>

/**
* Specifies schema names that must be generated even when listed in schemaMappings or importMappings.
* Specifies mapping-suppressed schemas to emit as isolated shadow models.
*
* Use `"*"` to include all mapping-suppressed schemas. Unmapped schemas remain in normal
* generation, and generated APIs and supporting-file metadata continue to use mapped classes.
* Supported families are Java, Groovy, Kotlin, C#, Python, Python Pydantic v1, PHP, Go client, Perl,
* PowerShell, R, and Ruby; other generators fail before writing files.
*/
@get:Optional
@get:Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,10 @@ public class CodeGenMojo extends AbstractMojo {
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.
* Mapping-suppressed schemas to emit as isolated shadow models.
* Use {@code <param>*</param>} as a wildcard to include all mapping-suppressed schemas.
* Supported families are Java, Groovy, Kotlin, C#, Python, Python Pydantic v1, PHP, Go client, Perl,
* PowerShell, R, and Ruby; other generators fail before writing files.
*/
@Parameter(name = "forcedGenerateSchemas", property = "openapi.generator.maven.plugin.forcedGenerateSchemas")
private List<String> forcedGenerateSchemas;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,22 @@ default List<Operation> divideOperationsByContentType(OpenAPI openAPI, String pa
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).
* Returns the set of schema names that must be generated even when suppressed by
* {@code schemaMapping}, or by a {@code typeMapping} with a matching {@code importMapping}.
* <p>
* A force-generated schema is emitted as an isolated shadow model under its <em>stock</em>
* (unmapped) model name — as if neither {@code schemaMapping} nor {@code importMapping} applied
* to it — while {@code typeMapping} is still honored. Shadow models may reference one another
* by their stock names, but they are not added to generated APIs or the normal model metadata
* used by supporting files. Ordinary generated code continues to use the mapped classes.
* <p>
* Use {@link CodegenConstants#FORCE_GENERATE_ALL_SCHEMAS} ({@code "*"}) as a wildcard
* to force-generate <em>all</em> mapped schemas at once.
* to force-generate all schemas that would otherwise be suppressed by mappings. Schemas without
* a suppressing mapping remain in the normal generation pass.
* <p>
* Supported generator families are Java, Groovy, Kotlin, C#, Python, Python Pydantic v1, PHP, Go
* client, Perl, PowerShell, R, and Ruby. Other generators reject this option before generating
* files.
*/
Set<String> forcedGenerateSchemas();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class CodegenConstants {

/**
* 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.
* all schemas suppressed by schema mappings or type-plus-import mappings are emitted as shadow
* models.
*/
public static final String FORCE_GENERATE_ALL_SCHEMAS = "*";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ 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.
// Mapping-suppressed schemas to emit as isolated shadow models.
// Use CodegenConstants.FORCE_GENERATE_ALL_SCHEMAS ("*") to include all suppressed 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<>();
Expand Down Expand Up @@ -1634,6 +1634,16 @@ public Set<String> forcedGenerateSchemas() {
return forcedGenerateSchemas;
}

public void clearModelNameCache() {
// reset the lazily-built model-name -> schema index so it is rebuilt with the current
// schemaMapping/importMapping state (used by the forced-schema generation pass).
modelNameToSchemaCache = null;
// drop cached CodegenProperty instances so property data types are rebuilt with the current
// mappings; otherwise a property resolved earlier (e.g. while generating apis) with the
// mapping intact would be reused during the forced shadow pass and leak the mapped name.
schemaCodegenPropertyCache.clear();
}

@Override
public Map<String, String> inlineSchemaNameMapping() {
return inlineSchemaNameMapping;
Expand Down
Loading
Loading