Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -26,6 +26,7 @@
import org.openapitools.codegen.meta.features.DocumentationFeature;
import org.openapitools.codegen.meta.features.SecurityFeature;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationsMap;

import java.io.File;
Expand Down Expand Up @@ -327,11 +328,37 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}
}

@Override
public ModelsMap postProcessModels(ModelsMap objs) {
return super.postProcessModels(objs);
}

@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
removeImport(objs, "java.util.List");
return objs;
}

@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
Map<String, ModelsMap> result = super.postProcessAllModels(objs);
for (ModelsMap modelsMap : result.values()) {
for (ModelMap modelMap : modelsMap.getModels()) {
CodegenModel model = modelMap.getModel();
if (model.parentModel != null) {
CodegenDiscriminator discriminator = model.parentModel.getDiscriminator();
if (discriminator != null) {
for (CodegenDiscriminator.MappedModel mappedModel : discriminator.getMappedModels()) {
if (mappedModel.getModelName().equals(model.name)) {
model.getVendorExtensions().put("x-discriminator-value", mappedModel.getMappingName());
break;
}
}
}
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {{javaxPackage}}.xml.bind.annotation.XmlEnumValue;
@Schema({{#title}}title="{{{.}}}", {{/title}}{{#description}}description="{{{.}}}"{{/description}}{{^description}}description=""{{/description}}){{/useSwaggerV3Annotations}}{{#useMicroProfileOpenAPIAnnotations}}
@org.eclipse.microprofile.openapi.annotations.media.Schema({{#title}}title="{{{.}}}", {{/title}}{{#description}}description="{{{.}}}"{{/description}}{{^description}}description=""{{/description}}){{/useMicroProfileOpenAPIAnnotations}}
{{#jackson}}
@JsonTypeName("{{name}}")
@JsonTypeName("{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}")
{{#additionalProperties}}
@JsonFormat(shape=JsonFormat.Shape.OBJECT)
{{/additionalProperties}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,38 @@ public void disableGenerateJsonCreator() throws Exception {
assertFileNotContains(files.get("RequiredProperties.java").toPath(), "@JsonCreator");
}

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

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/jaxrs/pestore.yaml", null, new ParseOptions()).getOpenAPI();
Comment thread
thiswasdumb marked this conversation as resolved.
Outdated

codegen.setOutputDir(output.getAbsolutePath());

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

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

// Parent model uses its own name
JavaFileAssert.assertThat(files.get("PetRequest.java"))
.fileContains("@JsonTypeName(\"PetRequest\")");

// Child models must use the discriminator mapping value (e.g. "CAT"), not the class name (e.g. "CatRequest")
JavaFileAssert.assertThat(files.get("CatRequest.java"))
.fileContains("@JsonTypeName(\"CAT\")")
.fileDoesNotContain("@JsonTypeName(\"CatRequest\")");

JavaFileAssert.assertThat(files.get("DogRequest.java"))
.fileContains("@JsonTypeName(\"DOG\")")
.fileDoesNotContain("@JsonTypeName(\"DogRequest\")");
}

@Test
public void testGenerateJsonNullableListFieldsHelperMethodReferences_issue23251() throws Exception {
Map<String, Object> properties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ paths:
- petstore_auth:
- 'read:pets'
deprecated: true
'/pet/request':
post:
operationId: createPetRequest
security: []
tags: [ pet ]
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PetRequest'
responses:
'201':
description: Pet created successfully
content:
application/json:
schema:
$ref: '#/components/schemas/PetRequest'
'/pet/{petId}':
get:
tags:
Expand Down Expand Up @@ -742,6 +760,37 @@ components:
- sold
xml:
name: Pet
PetRequest:
type: object
required:
- petType
properties:
petType:
type: string
name:
type: string
oneOf:
- $ref: '#/components/schemas/CatRequest'
- $ref: '#/components/schemas/DogRequest'
discriminator:
propertyName: petType
mapping:
CAT: '#/components/schemas/CatRequest'
DOG: '#/components/schemas/DogRequest'
CatRequest:
allOf:
- $ref: '#/components/schemas/PetRequest'
- type: object
properties:
indoor:
type: boolean
DogRequest:
allOf:
- $ref: '#/components/schemas/PetRequest'
- type: object
properties:
bark:
type: string
ApiResponse:
title: An uploaded response
description: Describes the result of uploading an image resource
Expand Down