Revert "[CORE] - Feature: `forcedGenerateSchemas` โ€” Force generation of schema-mapped or import-mapped schemas (#24066)

Merged
PI
2mo ago
+4-296

This reverts commit 9cebd4cf547.

Coming back to this after using it, the original push-back was right. A second generation pass
without the mappings (branch or dir diff, scoped via --global-property models=...) covers the
drift-check use case, and it runs in CI too, which was the main counter-argument at the time.

The forcedGenerateSchemas feature as shipped had two real problems:

  • It did not produce a faithful reference copy. Forcing generation did not bypass the mapping's
    rename (toModelName still applied), so mapping Category to ExternalCategory and forcing
    Category yielded ExternalCategory.java, not Category.java, the wrong class name to diff
    against a hand-written original.
  • It only worked in Java by accident. The single guard in DefaultGenerator was not coordinated
    with each generator's own schemaMapping handling and was only ever validated against Java. The
    importMappings half was not really wired either.

Making it behave correctly and consistently across all generators turned out to be very invasive,
and that complexity is not justified for roughly 4 lines of actual behavior against permanent API
surface across CLI, Maven, Gradle, and core. It shipped only in 7.24.0, so adoption is almost
certainly minimal and reverting #24066
now carries very little risk. The supported way to guard against drift between manually sideloaded
models and the spec-generated ones is the second-pass approach documented below.

Why

schemaMappings and importMappings mark a schema as already provided, so the generator
references the mapped type but does not emit it. Sometimes you still want the generator's own
version of a mapped schema, for example to produce a reference copy of a hand-written class that a
test can compare against.

You can get this with no custom option by running the generator a second time, into a separate
output directory, with the mappings removed and generation limited to the schemas you want.

Alternative solution

PassMappingsOutput
1, mainappliedyour real client/server (mapped schemas suppressed)
2, referenceomitted for the target schemasonly the target models, into a throwaway dir

Pass 2 uses the models global property to limit generation to a
colon-separated list of model names. Since the mappings are absent in that run, those models are
emitted normally. Keep the reference output in a temporary or test-only directory and never add it
as a source root.

CLI

Assume MyEnum and OtherSchema are normally schema-mapped to hand-written classes.

Pass 1, normal generation:

openapi-generator-cli generate -i api.yaml -g java -o ./out \
  --schema-mappings MyEnum=com.example.MyEnum,OtherSchema=com.example.OtherSchema

Pass 2, reference copy of just those schemas:

openapi-generator-cli generate -i api.yaml -g java -o ./out-reference \
  --global-property models=MyEnum:OtherSchema \
  --global-property modelTests=false,modelDocs=false,apis=false,apiTests=false,apiDocs=false,supportingFiles=false

To force-generate every mapped schema at once, drop the models= filter and run pass 2 with no
--schema-mappings / --import-mappings.

Maven

<executions>
  <!-- Pass 1: real generation, mapped schemas suppressed -->
  <execution>
    <id>main</id>
    <goals><goal>generate</goal></goals>
    <configuration>
      <inputSpec>${project.basedir}/api.yaml</inputSpec>
      <generatorName>java</generatorName>
      <output>${project.build.directory}/generated-sources/main</output>
      <schemaMappings>MyEnum=com.example.MyEnum,OtherSchema=com.example.OtherSchema</schemaMappings>
    </configuration>
  </execution>
  <!-- Pass 2: reference copy of the mapped schemas -->
  <execution>
    <id>reference</id>
    <goals><goal>generate</goal></goals>
    <configuration>
      <inputSpec>${project.basedir}/api.yaml</inputSpec>
      <generatorName>java</generatorName>
      <output>${project.build.directory}/generated-reference</output>
      <globalProperties>
        <models>MyEnum:OtherSchema</models>
        <apis>false</apis>
        <supportingFiles>false</supportingFiles>
        <modelTests>false</modelTests>
        <modelDocs>false</modelDocs>
        <apiTests>false</apiTests>
        <apiDocs>false</apiDocs>
      </globalProperties>
    </configuration>
  </execution>
</executions>

Gradle

import org.openapitools.generator.gradle.plugin.tasks.GenerateTask

openApiGenerate {
    inputSpec.set("$rootDir/api.yaml")
    generatorName.set("java")
    outputDir.set("$buildDir/generated/main")
    schemaMappings.set(mapOf(
        "MyEnum" to "com.example.MyEnum",
        "OtherSchema" to "com.example.OtherSchema",
    ))
}

tasks.register<GenerateTask>("generateReferenceSchemas") {
    inputSpec.set("$rootDir/api.yaml")
    generatorName.set("java")
    outputDir.set("$buildDir/generated/reference")
    globalProperties.set(mapOf(
        "models" to "MyEnum:OtherSchema",
        "apis" to "false",
        "supportingFiles" to "false",
        "modelTests" to "false",
        "modelDocs" to "false",
        "apiTests" to "false",
        "apiDocs" to "false",
    ))
}

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Reverts the forcedGenerateSchemas feature, restoring the old behavior where schemas listed in schemaMappings or importMappings are always skipped. Anyone using the option must remove it, and generated output may change if they relied on forced generation.

  • Removes the option from the CLI, Maven plugin, Gradle plugin, and core configuration.
  • Reverts DefaultGenerator to skip all schema-mapped models, including the wildcard * behavior.
  • Removes the feature's tests and documentation.