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 @@ -85,6 +85,7 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if serializabl
/**
* Set {v.name}
**/
@JsonProperty("{v.baseName}")
public void {v.setter}({v.datatypeWithEnum} {v.name}) {
this.{v.name} = {v.name};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
/**
* Set {v.name}
**/
@com.fasterxml.jackson.annotation.JsonProperty("{v.baseName}")
public void {v.setter}({v.datatypeWithEnum} {v.name}) {
this.{v.name} = {v.name};
}
Expand Down
93 changes: 93 additions & 0 deletions client/integration-tests/jsonproperty-getter-and-setter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
<groupId>io.quarkiverse.openapi.generator</groupId>
<version>3.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-openapi-generator-it-jsonproperty-getter-and-setter</artifactId>
<name>Quarkus - OpenAPI Generator - Integration Tests - Client - @JsonProperty for getters and setters</name>
<description>Ensures that @JsonProperty is properly added to getters and setters</description>

<dependencies>
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager
</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"openapi": "3.0.2",
"info": {
"title": "Animals - OpenAPI 3.0",
"version": "1.0.5"
},
"servers": [
{
"url": "/api/v3"
}
],
"tags": [
{
"name": "primate",
"description": "Everything about Primates"
}
],
"paths": {
"/primate/{id}": {
"get": {
"tags": [
"primate"
],
"summary": "Find primate by ID",
"description": "Returns a single primate",
"operationId": "getPrimateById",
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of primate to return",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
}
],
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Primate"
}
}
}
},
"400": {
"description": "Invalid ID supplied"
},
"404": {
"description": "Primate not found"
}
}
}
}
},
"components": {
"schemas": {
"Animal": {
"type": "object",
"properties": {
"born": {
"type": "string",
"description": "Dated Base extension.",
"format": "date-time"
},
"deceased": {
"type": "string",
"description": "Dated Base extension.",
"format": "date-time"
},
"animal_name": {
"type": "string",
"description": "Animal name"
}
},
"xml": {
"name": "animal"
}
},
"Mammal": {
"type": "object",
"allOf": [ {
"$ref": "#/components/schemas/Animal"
} ],
"properties": {
"gender": {
"type": "string",
"enum": [
"female",
"male"
]
}
},
"xml": {
"name": "mammal"
}
},
"Primate": {
"required": [
"name"
],
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Mammal"
}
],
"properties": {
"id": {
"type": "integer",
"format": "int64",
"example": 10
},
"name": {
"type": "string",
"example": "jane doe"
}
},
"xml": {
"name": "primate"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.openapi-generator.codegen.spec.jsonproperty_openapi_yaml.base-package=org.acme.jsonproperty.gettersetter
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package io.quarkiverse.openapi.generator.it;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

import jakarta.json.Json;
import jakarta.json.JsonObjectBuilder;

import org.acme.jsonproperty.gettersetter.model.Animal;
import org.acme.jsonproperty.gettersetter.model.Mammal;
import org.acme.jsonproperty.gettersetter.model.Primate;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
class JsonPropertyGetterSetterTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

@BeforeAll
static void setup() {
OBJECT_MAPPER.findAndRegisterModules();
}

@Test
void verifyGetterSetterWorksOnSnakeCasedFields() {
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
jsonObjectBuilder.add("animal_name", "Lion");

Animal animal;
try {
animal = OBJECT_MAPPER.readValue(jsonObjectBuilder.build().toString(), Animal.class);
} catch (Exception e) {
throw new RuntimeException(e);
}

assertEquals(Animal.class, animal.getClass());
assertEquals("Lion", animal.getAnimalName());
}

@Test
void verifyGetterSetterWorksOnNonSnakeCasedFields() {
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
// OffsetDateTime field to verify that non-snake-cased fields also work
jsonObjectBuilder.add("born", "2020-01-01T00:00:00Z");

Animal animal;
try {
animal = OBJECT_MAPPER.readValue(jsonObjectBuilder.build().toString(), Animal.class);
} catch (Exception e) {
throw new RuntimeException(e);
}

assertEquals(Animal.class, animal.getClass());
DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendInstant().toFormatter();
assertEquals("2020-01-01T00:00:00Z", animal.getBorn().format(formatter));
}

@Test
void verifyJavaReflectionIndicatesJsonPropertyOnSettersAndGetters() {
try {
var getter = Animal.class.getMethod("getAnimalName");
var setter = Animal.class.getMethod("setAnimalName", String.class);

var getterAnnotation = getter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);
var setterAnnotation = setter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);

assertEquals("animal_name", getterAnnotation.value());
assertEquals("animal_name", setterAnnotation.value());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
try {
var getter = Primate.class.getMethod("getAnimalName");
var setter = Primate.class.getMethod("setAnimalName", String.class);

var getterAnnotation = getter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);
var setterAnnotation = setter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);

assertEquals("animal_name", getterAnnotation.value());
assertEquals("animal_name", setterAnnotation.value());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
try {
var getter = Mammal.class.getMethod("getAnimalName");
var setter = Mammal.class.getMethod("setAnimalName", String.class);

var getterAnnotation = getter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);
var setterAnnotation = setter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);

assertEquals("animal_name", getterAnnotation.value());
assertEquals("animal_name", setterAnnotation.value());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

@Test
void verifyJavaReflectionIndicatesJsonPropertyOnSettersAndGettersQueryParam() {
try {
var getter = Animal.AnimalQueryParam.class.getMethod("getAnimalName");
var setter = Animal.AnimalQueryParam.class.getMethod("setAnimalName", String.class);

var getterAnnotation = getter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);
var setterAnnotation = setter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);

assertEquals("animal_name", getterAnnotation.value());
assertEquals("animal_name", setterAnnotation.value());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}

try {
var getter = Mammal.MammalQueryParam.class.getMethod("getAnimalName");
var setter = Mammal.MammalQueryParam.class.getMethod("setAnimalName", String.class);

var getterAnnotation = getter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);
var setterAnnotation = setter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);

assertEquals("animal_name", getterAnnotation.value());
assertEquals("animal_name", setterAnnotation.value());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}

try {
var getter = Primate.PrimateQueryParam.class.getMethod("getAnimalName");
var setter = Primate.PrimateQueryParam.class.getMethod("setAnimalName", String.class);

var getterAnnotation = getter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);
var setterAnnotation = setter.getAnnotation(com.fasterxml.jackson.annotation.JsonProperty.class);

assertEquals("animal_name", getterAnnotation.value());
assertEquals("animal_name", setterAnnotation.value());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
1 change: 1 addition & 0 deletions client/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<module>serializable-model</module>
<module>equals-hashcode</module>
<module>override-credential-provider</module>
<module>jsonproperty-getter-and-setter</module>
</modules>
<dependencyManagement>
<dependencies>
Expand Down