Skip to content

Commit 5651547

Browse files
committed
Fix issue 22209: [JAVA-gen] Fix oneOf inheritance: prevent generation of abstract wrapper and ensure real POJO model is produced
- Add useOneOfPojo option to generate real POJO classes for oneOf schemas instead of wrapper models - Override updateModelForComposedSchema() to restore original vars when option is enabled - Process the option in processOpts() method with default value false for backward compatibility fixes #22209
1 parent cc9cf08 commit 5651547

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
114114

115115
public static final String CAMEL_CASE_DOLLAR_SIGN = "camelCaseDollarSign";
116116
public static final String USE_ONE_OF_INTERFACES = "useOneOfInterfaces";
117+
public static final String USE_ONE_OF_POJO = "useOneOfPojo";
117118
public static final String LOMBOK = "lombok";
118119
public static final String DEFAULT_TEST_FOLDER = "${project.build.directory}/generated-test-sources/openapi";
119120
public static final String GENERATE_CONSTRUCTOR_WITH_ALL_ARGS = "generateConstructorWithAllArgs";
@@ -343,6 +344,7 @@ public AbstractJavaCodegen() {
343344
cliOptions.add(CliOption.newBoolean(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC, this.isHideGenerationTimestamp()));
344345
cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));
345346
cliOptions.add(CliOption.newBoolean(USE_ONE_OF_INTERFACES, "whether to use a java interface to describe a set of oneOf options, where each option is a class that implements the interface"));
347+
cliOptions.add(CliOption.newBoolean(USE_ONE_OF_POJO, "Generate real POJO classes for oneOf schemas instead of wrapper models").defaultValue("false"));
346348

347349
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use").defaultValue(this.getDateLibrary());
348350
Map<String, String> dateOptions = new HashMap<>();
@@ -402,6 +404,31 @@ public AbstractJavaCodegen() {
402404
cliOptions.add(CliOption.newString(CodegenConstants.DEFAULT_TO_EMPTY_CONTAINER, CodegenConstants.DEFAULT_TO_EMPTY_CONTAINER_DESC));
403405
}
404406

407+
/**
408+
* Do not merge properties for ComposedSchema avec schema.getOneOf() not empty
409+
*
410+
* {@inheritDoc}
411+
* @see org.openapitools.codegen.DefaultCodegen#updateModelForComposedSchema(org.openapitools.codegen.CodegenModel, io.swagger.v3.oas.models.media.Schema, java.util.Map)
412+
*/
413+
@SuppressWarnings("rawtypes")
414+
@Override
415+
protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<String, Schema> allDefinitions)
416+
{
417+
final List<CodegenProperty> varsOrigin = new ArrayList<>(m.vars);
418+
final List<CodegenProperty> optionalVarsOrigin = new ArrayList<>(m.optionalVars);
419+
420+
super.updateModelForComposedSchema(m, schema, allDefinitions);
421+
422+
if(schema.getOneOf() != null && !schema.getOneOf().isEmpty() ) {
423+
LOGGER.info("Do not merge properties for ComposedSchema avec schema.getOneOf() not empty {} -> {} ", m.getClassname(), schema.getOneOf());
424+
if (Boolean.TRUE.equals(additionalProperties.get(USE_ONE_OF_POJO))) {
425+
m.vars = varsOrigin;
426+
m.optionalVars = optionalVarsOrigin;
427+
m.oneOf = new TreeSet<>();
428+
}
429+
}
430+
}
431+
405432
@Override
406433
public void processOpts() {
407434
useCodegenAsMustacheParentContext();
@@ -447,6 +474,17 @@ public void processOpts() {
447474
additionalProperties.put(ANNOTATION_LIBRARY, AnnotationLibrary.NONE);
448475
}
449476

477+
// -----------------------------------------------------------------
478+
// `useOneOfPojo` – backward compatibility switch.
479+
// set to true to get the POJO
480+
// -----------------------------------------------------------------
481+
if (additionalProperties.containsKey(USE_ONE_OF_POJO)) {
482+
Boolean flag = Boolean.valueOf(additionalProperties.get(USE_ONE_OF_POJO).toString());
483+
additionalProperties.put(USE_ONE_OF_POJO, flag);
484+
} else {
485+
additionalProperties.put(USE_ONE_OF_POJO, Boolean.FALSE);
486+
}
487+
450488
convertPropertyToBooleanAndWriteBack(GENERATE_CONSTRUCTOR_WITH_ALL_ARGS, this::setGenerateConstructorWithAllArgs);
451489
convertPropertyToBooleanAndWriteBack(GENERATE_BUILDERS, this::setGenerateBuilders);
452490
convertPropertyToBooleanAndWriteBack(DISABLE_DISCRIMINATOR_JSON_IGNORE_PROPERTIES, this::setDisableDiscriminatorJsonIgnoreProperties);

0 commit comments

Comments
 (0)