Skip to content

Fix issue #15298 #15299

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 3 commits 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 @@ -3147,8 +3147,12 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
* @param sc The Schema that may contain the discriminator
* @param discPropName The String that is the discriminator propertyName in the schema
*/
private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, String discPropName, OpenAPI openAPI) {
private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, String discPropName, OpenAPI openAPI, Set<Schema> visitedSchemas) {
Schema refSchema = ModelUtils.getReferencedSchema(openAPI, sc);
if (visitedSchemas.contains(refSchema)) {
return null;
}
visitedSchemas.add(refSchema);
if (refSchema.getProperties() != null && refSchema.getProperties().get(discPropName) != null) {
Schema discSchema = (Schema) refSchema.getProperties().get(discPropName);
CodegenProperty cp = new CodegenProperty();
Expand All @@ -3166,7 +3170,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
if (composedSchema.getAllOf() != null) {
// If our discriminator is in one of the allOf schemas break when we find it
for (Schema allOf : composedSchema.getAllOf()) {
CodegenProperty cp = discriminatorFound(composedSchemaName, allOf, discPropName, openAPI);
CodegenProperty cp = discriminatorFound(composedSchemaName, allOf, discPropName, openAPI, visitedSchemas);
if (cp != null) {
return cp;
}
Expand All @@ -3177,7 +3181,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
CodegenProperty cp = new CodegenProperty();
for (Schema oneOf : composedSchema.getOneOf()) {
String modelName = ModelUtils.getSimpleRef(oneOf.get$ref());
CodegenProperty thisCp = discriminatorFound(composedSchemaName, oneOf, discPropName, openAPI);
CodegenProperty thisCp = discriminatorFound(composedSchemaName, oneOf, discPropName, openAPI, visitedSchemas);
if (thisCp == null) {
LOGGER.warn(
"'{}' defines discriminator '{}', but the referenced OneOf schema '{}' is missing {}",
Expand All @@ -3200,7 +3204,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
CodegenProperty cp = new CodegenProperty();
for (Schema anyOf : composedSchema.getAnyOf()) {
String modelName = ModelUtils.getSimpleRef(anyOf.get$ref());
CodegenProperty thisCp = discriminatorFound(composedSchemaName, anyOf, discPropName, openAPI);
CodegenProperty thisCp = discriminatorFound(composedSchemaName, anyOf, discPropName, openAPI, visitedSchemas);
if (thisCp == null) {
LOGGER.warn(
"'{}' defines discriminator '{}', but the referenced AnyOf schema '{}' is missing {}",
Expand Down Expand Up @@ -3355,7 +3359,8 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
"Invalid inline schema defined in oneOf/anyOf in '{}'. Per the OpenApi spec, for this case when a composed schema defines a discriminator, the oneOf/anyOf schemas must use $ref. Change this inline definition to a $ref definition",
composedSchemaName);
}
CodegenProperty df = discriminatorFound(composedSchemaName, sc, discPropName, openAPI);
HashSet<Schema> visitedSchemas = new HashSet<>();
CodegenProperty df = discriminatorFound(composedSchemaName, sc, discPropName, openAPI, visitedSchemas);
String modelName = ModelUtils.getSimpleRef(ref);
if (df == null || !df.isString || df.required != true) {
String msgSuffix = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,37 @@ public void testEnumCaseSensitive_issue8084() throws IOException {
.bodyContainsLines("if (b.value.equals(value)) {");
}

@Test
public void shouldNotFallWithNPEOrStackOverflow() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_spring_codegen_polymorphism.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime");
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
codegen.additionalProperties().put(USE_ONE_OF_INTERFACES, "false");
codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, "true");
codegen.additionalProperties().put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true");
codegen.additionalProperties().put(USE_TAGS, "true");

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.LIBRARY, "spring-cloud");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();
}

@Test
public void multiLineOperationDescription() throws IOException {
Expand All @@ -2692,4 +2722,5 @@ public void multiLineTagDescription() throws IOException {
JavaFileAssert.assertThat(files.get("PingTagApi.java"))
.fileContains("This is a multine tag : * tag item 1 * tag item 2 ");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
openapi: 3.0.2
info:
title: Polymorphism Java Example
version: 1.0.0
contact:
name: polydectes
email: 12345@sharklasers.com
servers:
- url: 'https://localhost'
tags:
- name: Example
paths:
'/animal':
post:
tags:
- Example
operationId: createAnAnimal
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Animal'
responses:
'200':
$ref: '#/components/responses/200-OK'
'400':
$ref: '#/components/responses/400-BadRequest'
components:
responses:
200-OK:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Animal'
400-BadRequest:
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
schemas:
Animal:
type: object
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: animalType
Cat:
type: object
allOf:
- $ref: '#/components/schemas/Animal'
properties:
acceptableAsHousepet:
type: boolean
Dog:
type: object
allOf:
- $ref: '#/components/schemas/Animal'
properties:
canBeTrained:
type: string
OkResponse:
type: object
properties:
id:
type: string
ErrorResponse:
type: object
properties:
message:
type: string