Skip to content

Commit

Permalink
Merge pull request #1671 from altro3/fix-oneof-in-generator
Browse files Browse the repository at this point in the history
Fix generating classes for oneOf block without discriminator
  • Loading branch information
altro3 authored Aug 5, 2024
2 parents 5355de6 + 2202d10 commit 62cdfd1
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {
{{/formatNoEmptyLines}}

{{#discriminator}}
{{#discriminator}}
public {{propertyType}} {{propertyGetter}}();
{{/discriminator}}
{{/discriminator}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
{{#generatedAnnotation}}
{{>common/generatedAnnotation}}
{{/generatedAnnotation}}
{{#discriminator}}
{{>common/model/typeInfoAnnotation}}
{{/discriminator}}
{{>common/model/xmlAnnotation}}
{{#useBeanValidation}}
{{^micronaut_serde_jackson}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{{#jackson}}
{{#discriminator}}
{{#discriminator.propertyBaseName}}
{{#jackson}}
@JsonIgnoreProperties(
value = "{{{discriminator.propertyBaseName}}}", // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "{{{discriminator.propertyBaseName}}}", visible = true)
{{#discriminator}}
{{#vendorExtensions.hasMappedModels}}
{{#vendorExtensions.hasMultipleMappedModels}}
{{#vendorExtensions.hasMappedModels}}
{{#vendorExtensions.hasMultipleMappedModels}}
@JsonSubTypes({{openbrace}}{{#mappedModels}}
@JsonSubTypes.Type(value = {{modelName}}.class, name = "{{^vendorExtensions.x-discriminator-value}}{{mappingName}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"){{^-last}},{{/-last}}
{{/mappedModels}}{{closebrace}})
{{/vendorExtensions.hasMultipleMappedModels}}
{{^vendorExtensions.hasMultipleMappedModels}}
{{/vendorExtensions.hasMultipleMappedModels}}
{{^vendorExtensions.hasMultipleMappedModels}}
@JsonSubTypes({{#mappedModels}}@JsonSubTypes.Type(value = {{modelName}}.class, name = "{{^vendorExtensions.x-discriminator-value}}{{mappingName}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"){{/mappedModels}})
{{/vendorExtensions.hasMultipleMappedModels}}
{{/vendorExtensions.hasMappedModels}}
{{/vendorExtensions.hasMultipleMappedModels}}
{{/vendorExtensions.hasMappedModels}}
{{/jackson}}
{{/discriminator.propertyBaseName}}
{{/discriminator}}
{{/jackson}}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} : {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {
{{/formatNoEmptyLines}}

{{#discriminator}}
{{#discriminator}}
val {{propertyName}}: {{propertyType}}
{{/discriminator}}
{{/discriminator}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
{{#generatedAnnotation}}
{{>common/generatedAnnotation}}
{{/generatedAnnotation}}
{{#discriminator}}
{{>common/model/typeInfoAnnotation}}
{{/discriminator}}
{{>common/model/xmlAnnotation}}
{{#useBeanValidation}}
{{^micronaut_serde_jackson}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{{#jackson}}
{{#discriminator}}
{{#discriminator.propertyBaseName}}
{{#jackson}}
@JsonIgnoreProperties(
value = ["{{{discriminator.propertyBaseName}}}"], // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "{{{discriminator.propertyBaseName}}}", visible = true)
{{#discriminator}}
{{#vendorExtensions.hasMappedModels}}
{{#vendorExtensions.hasMappedModels}}
@JsonSubTypes({{#mappedModels}}
JsonSubTypes.Type(value = {{modelName}}::class, name = "{{^vendorExtensions.x-discriminator-value}}{{mappingName}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"){{^-last}},{{/-last}}
{{/mappedModels}})
{{/vendorExtensions.hasMappedModels}}
{{/vendorExtensions.hasMappedModels}}
{{/jackson}}
{{/discriminator.propertyBaseName}}
{{/discriminator}}
{{/jackson}}
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,16 @@ void testOneOf() {
assertFileContains(path + "model/Subject.java", "public String getTypeCode();");
assertFileContains(path + "model/Person.java", "public String getTypeCode() {");
}

@Test
void testOneOfWithoutDiscriminator() {

var codegen = new JavaMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/oneof-without-discriminator.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileNotContains(path + "model/OrderDTOShoppingNotes.java", "@JsonIgnoreProperties(",
"@JsonTypeInfo"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,16 @@ void testOneOf() {
assertFileContains(path + "model/Subject.kt", "val typeCode: String");
assertFileContains(path + "model/Person.kt", "override var typeCode: String = \"PERS\",");
}

@Test
void testOneOfWithoutDiscriminator() {

var codegen = new KotlinMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/oneof-without-discriminator.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileNotContains(path + "model/OrderDTOShoppingNotes.kt", "@JsonIgnoreProperties(",
"@JsonTypeInfo"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
openapi: 3.0.0
info:
title: 'Order API'
version: v6
servers:
-
url: 'https://{environment}.com/api/{apiVersion}'
variables:
environment:
enum:
- api-dev.test.com
default: api.dev.test.com
apiVersion:
enum:
- v1
- v2
- v3
- v4
- v5
- v6
- v7
default: v6
paths:
'/orders/{id}':
get:
tags:
- Order
operationId: getOrderById
parameters:
-
$ref: '#/components/parameters/OrderId'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/OrderDTO'
components:
parameters:
OrderId:
name: id
in: path
description: 'The ID'
required: true
schema:
type: string
schemas:
OrderDTO:
required:
- id
properties:
id:
type: string
shopping_notes:
nullable: true
oneOf:
- $ref: '#/components/schemas/ShoppingNotesDTO'
ShoppingNotesDTO:
properties:
message:
type: string
icon_url:
type: string
type: object

0 comments on commit 62cdfd1

Please sign in to comment.