Skip to content

Issue 16394: Fix uncompileable java client code from composed schema #16478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -561,6 +561,9 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
}
}




return objs;
}

Expand Down Expand Up @@ -3576,12 +3579,13 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
.orElseGet(() -> typeMapping.get("string"));
discriminator.setPropertyType(propertyType);

// check to see if the discriminator property is an enum string
if (schema.getProperties() != null &&
schema.getProperties().get(discriminatorPropertyName) instanceof StringSchema) {
StringSchema s = (StringSchema) schema.getProperties().get(discriminatorPropertyName);
if (s.getEnum() != null && !s.getEnum().isEmpty()) { // it's an enum string
discriminator.setIsEnum(true);

// sometimes the discriminator is directly described in the properties
discriminator.setIsEnum(discriminatorIsEnumString(schema, discriminatorPropertyName));
if (ModelUtils.isComposedSchema(schema)) {
// other times the properties are null and only the parent contains data about if the discriminator is an enum
for(Schema s: getModelNameToSchemaCache().values()) {
discriminator.setIsEnum(discriminatorIsEnumString(s, discriminatorPropertyName));
}
}

Expand Down Expand Up @@ -3639,6 +3643,16 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
return discriminator;
}

private static boolean discriminatorIsEnumString(Schema schema, String discriminatorPropertyName) {
if (schema.getProperties() != null && schema.getProperties().get(discriminatorPropertyName) instanceof StringSchema) {
StringSchema s = (StringSchema) schema.getProperties().get(discriminatorPropertyName);
if (s.getEnum() != null && !s.getEnum().isEmpty()) { // it's an enum string
return true;
}
}
return false;
}

/**
* Handle the model for the 'additionalProperties' keyword in the OAS schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.languages.AbstractJavaCodegen;
import org.openapitools.codegen.languages.JavaClientCodegen;
Expand Down Expand Up @@ -2384,4 +2385,32 @@ public void testEnumCaseSensitive_issue8084() throws IOException {
.bodyContainsLines("if (b.value.equals(value)) {");
}

@Test
public void testIfJavaCodeWithDiscriminatorWillCompile_issue16394() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
properties.put(JavaClientCodegen.OKHTTP_GSON, true);

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setInputSpec("src/test/resources/bugs/issue_16394.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

GlobalSettings.setProperty("debugModels", "true");
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);

for(File f: files) {
// a rather fragile way of determining if the code won't compile. a better approach would be a reusable
// utility to determine this, similar to how validateJavaSourceFiles checks the syntax.
if("Cat.java".equals(f.getName())) {
String fileContents = new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
Assert.assertFalse(fileContents.contains("this.petType = this.getClass().getSimpleName()"));
}
}
}
}
63 changes: 63 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_16394.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
openapi: 3.0.3
info:
title: example of a parent schema with discriminator property that is an enum
version: 2.0
paths:
"/pet/{petId}":
get:
summary: Find pet by id
operationId: getBetById
parameters:
- name: petId
in: path
required: true
schema:
type: string
responses:
'200':
description: OK - The request has succeeded.
content:
application/json:
schema:
type: array
items:
"$ref": "#/components/schemas/Pet"
components:
schemas:
Pet:
type: object
required:
- petType
properties:
petType:
type: string
enum: [ Cat, Dog, Lizard ]
discriminator:
propertyName: petType
mapping:
dog: Dog
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Cat`
properties:
name:
type: string
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Dog`
properties:
bark:
type: string
Lizard:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Lizard`
properties:
lovesRocks:
type: boolean