forked from loafle/openapi-generator-original
Adds fix for issue7262 and a test of it (#7328)
* Adds fix for issue7262 and a test of it * Updates inline schema to not include properties * Adds null check of discriminator * Updates issue sample file
This commit is contained in:
@@ -2826,9 +2826,8 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
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)) {
|
||||
|
||||
@@ -33,7 +33,6 @@ import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||
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;
|
||||
@@ -2231,4 +2230,27 @@ public class DefaultCodegenTest {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
Reference in New Issue
Block a user