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 @@ -7,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
**/
{/if}
{#include additionalModelTypeAnnotations.qute m=m/}
{#if m.discriminator && m.discriminator.mappedModels && !m.discriminator.mappedModels.empty}
{#if m.discriminator && m.discriminator.mappedModels && !m.discriminator.mappedModels.empty && m.children}
@com.fasterxml.jackson.annotation.JsonIgnoreProperties(
value = "{m.discriminator.propertyBaseName}", // ignore manually set {m.discriminator.propertyBaseName}, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the {m.discriminator.propertyBaseName} to be set during deserialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ void verifyOAuthDuplicateAnnotationOnCompositeAuthProvider() throws URISyntaxExc
assertThat(oauthAnnotationsCount).isEqualTo(1);
}

/**
* If the specification component has `oneOf` specified instead of `allOf`, the inner generator won't create a hierarchy of
* classes.
* In this situation, we can't generate the `JsonSubTypes` annotations.
*/
@Test
void verifyDiscriminatorGeneration() throws java.net.URISyntaxException, FileNotFoundException {
void verifyOneOfDiscriminatorGeneration() throws java.net.URISyntaxException, FileNotFoundException {
OpenApiClientGeneratorWrapper generatorWrapper = createGeneratorWrapper("issue-852.json");
final List<File> generatedFiles = generatorWrapper.generate("org.issue852");

Expand All @@ -96,6 +101,30 @@ void verifyDiscriminatorGeneration() throws java.net.URISyntaxException, FileNot
.filter(f -> f.getName().endsWith("PostRevisionForDocumentRequest.java")).findFirst();
assertThat(classWithDiscriminator).isPresent();

final CompilationUnit compilationUnit = StaticJavaParser.parse(classWithDiscriminator.orElseThrow());
assertThat(compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
.flatMap(first -> first.getAnnotationByClass(com.fasterxml.jackson.annotation.JsonSubTypes.class)))
.isNotPresent();
}

/**
* Only generates `JsonSubTypes` annotations in case the class has children, otherwise skip since Jackson will complain in
* runtime.
* The file issue-1022.json is a classic example of a spec with allOf to denote how the Java POJO should be and how Jackson
* would serialize.
*/
@Test
void verifyAllOfDiscriminatorGeneration() throws java.net.URISyntaxException, FileNotFoundException {
OpenApiClientGeneratorWrapper generatorWrapper = createGeneratorWrapper("issue-1022.json");
final List<File> generatedFiles = generatorWrapper.generate("org.issue1022");

assertNotNull(generatedFiles);
assertFalse(generatedFiles.isEmpty());

final Optional<File> classWithDiscriminator = generatedFiles.stream()
.filter(f -> f.getName().startsWith("Thing.java")).findFirst();
assertThat(classWithDiscriminator).isPresent();

final CompilationUnit compilationUnit = StaticJavaParser.parse(classWithDiscriminator.orElseThrow());
assertThat(compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)
.flatMap(first -> first.getAnnotationByClass(com.fasterxml.jackson.annotation.JsonSubTypes.class)))
Expand Down
118 changes: 118 additions & 0 deletions client/deployment/src/test/resources/openapi/issue-1022.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"openapi": "3.0.3",
"info": {
"title": "tuto API",
"version": "1.0.0-SNAPSHOT"
},
"servers": [
{
"url": "http://localhost:8080",
"description": "Auto generated value"
},
{
"url": "http://0.0.0.0:8080",
"description": "Auto generated value"
}
],
"paths": {
"/": {
"get": {
"tags": [
"Default"
],
"operationId": "get",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Data"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Data": {
"required": [
"things"
],
"type": "object",
"properties": {
"things": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Thing"
},
"anyOf": [
{
"$ref": "#/components/schemas/SomeThing"
},
{
"$ref": "#/components/schemas/OtherThing"
}
]
}
}
},
"OtherThing": {
"description": "Other thing",
"required": [
"other"
],
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Thing"
}
],
"properties": {
"other": {
"type": "string"
}
}
},
"SomeThing": {
"description": "Some thing",
"required": [
"some"
],
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Thing"
}
],
"properties": {
"some": {
"type": "string"
}
}
},
"Thing": {
"description": "Thing",
"required": [
"thing"
],
"type": "object",
"properties": {
"thing": {
"type": "string"
}
},
"discriminator": {
"propertyName": "@type",
"mapping": {
"SomeThing": "#/components/schemas/SomeThing",
"OtherThing": "#/components/schemas/OtherThing"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[.configuration-legend]
icon:lock[title=Fixed at build time] Configuration property fixed at build time - All other configuration properties are overridable at runtime
[.configuration-reference.searchable, cols="80,.^10,.^10"]
|===

h|[.header-title]##Configuration property##
h|Type
h|Default

3+|No configuration properties found.

|===