Skip to content
Merged
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 @@ -2826,9 +2826,8 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
if (ref == null) {
// for schemas with no ref, it is not possible to build the discriminator map
// because ref is how we get the model name
// we only hit this use case for a schema with inline composed schemas, and one of those
// schemas also has inline composed schemas
throw new RuntimeException("Invalid inline schema defined in allOf in '" + childName + "'. Per the OpenApi spec, for this case when a composed schema defines a discriminator, the allOf schemas must use $ref. Change this inline definition to a $ref definition");
// we hit this use case when an allOf composed schema contains an inline schema
continue;
}
String parentName = ModelUtils.getSimpleRef(ref);
if (parentName.equals(currentSchemaName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.parser.core.models.ParseOptions;

import org.openapitools.codegen.languages.JavaClientCodegen;
import org.openapitools.codegen.templating.mustache.CamelCaseLambda;
import org.openapitools.codegen.templating.mustache.IndentedLambda;
import org.openapitools.codegen.templating.mustache.LowercaseLambda;
Expand Down Expand Up @@ -2231,4 +2230,27 @@ public void testFormComposedSchema() {
assertTrue(names.contains("passwordConfirmation"));
assertTrue(names.contains("oldPassword"));
}

@Test
public void inlineAllOfSchemaDoesNotThrowException() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue7262.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

String modelName = "UserTimeBase";
Schema sc = openAPI.getComponents().getSchemas().get(modelName);
CodegenModel cm = codegen.fromModel(modelName, sc);

final Set<CodegenDiscriminator.MappedModel> expectedMappedModels = new HashSet<>(Arrays.asList(new CodegenDiscriminator.MappedModel("UserSleep", "UserSleep")));
final Set<CodegenDiscriminator.MappedModel> mappedModels = cm.getDiscriminator().getMappedModels();
assertEquals(mappedModels, expectedMappedModels);

modelName = "UserSleep";
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
final Set<String> expectedAllOf = new HashSet<>(Arrays.asList("UserTimeBase"));
assertEquals(cm.allOf, expectedAllOf);
assertEquals(openAPI.getComponents().getSchemas().size(), 2);
assertNull(cm.getDiscriminator());
}
}
38 changes: 38 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue7262.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
openapi: "3.0.1"
info:
version: 1.0.0
title: allOf discriminator issue
paths:
/sleep:
patch:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/UserSleep'
responses:
'200':
description: Updated
components:
schemas:
UserSleep:
allOf:
- $ref: '#/components/schemas/UserTimeBase'
- type: object
additionalProperties: true
UserTimeBase:
required:
- "$type"
type: object
properties:
"$type":
type: string
start:
type: string
nullable: true
end:
type: string
nullable: true
additionalProperties: true
discriminator:
propertyName: "$type"