Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spring generator dto annotations for xml support #17054

Merged
merged 1 commit into from
Jan 9, 2024
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
1 change: 1 addition & 0 deletions bin/configs/spring-boot-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ additionalProperties:
snapshotVersion: "true"
useSpringBoot3: true
useBeanValidation: true
withXml: true
hideGenerationTimestamp: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/mostInnerItems}}
{{/isContainer}}
{{/isEnum}}
{{#jackson}}
{{#withXml}}
@JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
{{/withXml}}
{{/jackson}}
{{#gson}}
@SerializedName("{{baseName}}")
{{/gson}}
Expand Down Expand Up @@ -199,6 +194,9 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/swagger1AnnotationLibrary}}
{{#jackson}}
@JsonProperty("{{baseName}}")
{{#withXml}}
@JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
{{/withXml}}
{{/jackson}}
{{#deprecated}}
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,7 @@ public void shouldGenerateJsonPropertyAnnotationLocatedInGetters_issue5705() thr
.readLocation("src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.setWithXml(true);
codegen.setOutputDir(output.getAbsolutePath());

ClientOptInput input = new ClientOptInput()
Expand All @@ -2564,34 +2565,42 @@ public void shouldGenerateJsonPropertyAnnotationLocatedInGetters_issue5705() thr
.hasProperty("normalPropertyName")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.doesNotContainsWithName("JacksonXmlProperty")
.toProperty().toType()
.hasProperty("UPPER_CASE_PROPERTY_SNAKE")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.doesNotContainsWithName("JacksonXmlProperty")
.toProperty().toType()
.hasProperty("lowerCasePropertyDashes")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.doesNotContainsWithName("JacksonXmlProperty")
.toProperty().toType()
.hasProperty("propertyNameWithSpaces")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.doesNotContainsWithName("JacksonXmlProperty")
.toProperty().toType()
.assertMethod("getNormalPropertyName")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"normalPropertyName\""))
.containsWithNameAndAttributes("JacksonXmlProperty", ImmutableMap.of("localName", "\"normalPropertyName\""))
.toMethod().toFileAssert()
.assertMethod("getUPPERCASEPROPERTYSNAKE")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"UPPER_CASE_PROPERTY_SNAKE\""))
.containsWithNameAndAttributes("JacksonXmlProperty", ImmutableMap.of("localName", "\"UPPER_CASE_PROPERTY_SNAKE\""))
.toMethod().toFileAssert()
.assertMethod("getLowerCasePropertyDashes")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"lower-case-property-dashes\""))
.containsWithNameAndAttributes("JacksonXmlProperty", ImmutableMap.of("localName", "\"lower-case-property-dashes\""))
.toMethod().toFileAssert()
.assertMethod("getPropertyNameWithSpaces")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"property name with spaces\""));
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"property name with spaces\""))
.containsWithNameAndAttributes("JacksonXmlProperty", ImmutableMap.of("localName", "\"property name with spaces\""));
}

@Test
Expand Down
9 changes: 9 additions & 0 deletions samples/openapi3/server/petstore/springboot-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<!-- XML processing: Jackson -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import jakarta.xml.bind.annotation.*;

import java.util.*;
import jakarta.annotation.Generated;
Expand All @@ -19,6 +22,10 @@
*/

@Schema(name = "Category", description = "A category for a pet")
@JacksonXmlRootElement(localName = "Category")
@XmlRootElement(name = "Category")
@XmlAccessorType(XmlAccessType.FIELD)

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Category {

Expand All @@ -38,6 +45,7 @@ public Category id(Long id) {

@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("id")
@JacksonXmlProperty(localName = "id")
public Long getId() {
return id;
}
Expand All @@ -58,6 +66,7 @@ public Category name(String name) {
@Pattern(regexp = "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")
@Schema(name = "name", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("name")
@JacksonXmlProperty(localName = "name")
public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import jakarta.xml.bind.annotation.*;

import java.util.*;
import jakarta.annotation.Generated;
Expand All @@ -21,6 +24,10 @@

@Schema(name = "ApiResponse", description = "Describes the result of uploading an image resource")
@JsonTypeName("ApiResponse")
@JacksonXmlRootElement(localName = "ModelApiResponse")
@XmlRootElement(name = "ModelApiResponse")
@XmlAccessorType(XmlAccessType.FIELD)

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class ModelApiResponse {

Expand All @@ -42,6 +49,7 @@ public ModelApiResponse code(Integer code) {

@Schema(name = "code", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("code")
@JacksonXmlProperty(localName = "code")
public Integer getCode() {
return code;
}
Expand All @@ -62,6 +70,7 @@ public ModelApiResponse type(String type) {

@Schema(name = "type", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("type")
@JacksonXmlProperty(localName = "type")
public String getType() {
return type;
}
Expand All @@ -82,6 +91,7 @@ public ModelApiResponse message(String message) {

@Schema(name = "message", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("message")
@JacksonXmlProperty(localName = "message")
public String getMessage() {
return message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import jakarta.xml.bind.annotation.*;

import java.util.*;
import jakarta.annotation.Generated;
Expand All @@ -22,6 +25,10 @@
*/

@Schema(name = "Order", description = "An order for a pets from the pet store")
@JacksonXmlRootElement(localName = "Order")
@XmlRootElement(name = "Order")
@XmlAccessorType(XmlAccessType.FIELD)

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Order {

Expand Down Expand Up @@ -87,6 +94,7 @@ public Order id(Long id) {

@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("id")
@JacksonXmlProperty(localName = "id")
public Long getId() {
return id;
}
Expand All @@ -107,6 +115,7 @@ public Order petId(Long petId) {

@Schema(name = "petId", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("petId")
@JacksonXmlProperty(localName = "petId")
public Long getPetId() {
return petId;
}
Expand All @@ -127,6 +136,7 @@ public Order quantity(Integer quantity) {

@Schema(name = "quantity", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("quantity")
@JacksonXmlProperty(localName = "quantity")
public Integer getQuantity() {
return quantity;
}
Expand All @@ -147,6 +157,7 @@ public Order shipDate(OffsetDateTime shipDate) {
@Valid
@Schema(name = "shipDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("shipDate")
@JacksonXmlProperty(localName = "shipDate")
public OffsetDateTime getShipDate() {
return shipDate;
}
Expand All @@ -167,6 +178,7 @@ public Order status(StatusEnum status) {

@Schema(name = "status", description = "Order Status", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("status")
@JacksonXmlProperty(localName = "status")
public StatusEnum getStatus() {
return status;
}
Expand All @@ -187,6 +199,7 @@ public Order complete(Boolean complete) {

@Schema(name = "complete", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("complete")
@JacksonXmlProperty(localName = "complete")
public Boolean getComplete() {
return complete;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import jakarta.xml.bind.annotation.*;

import java.util.*;
import jakarta.annotation.Generated;
Expand All @@ -25,6 +28,10 @@
*/

@Schema(name = "Pet", description = "A pet for sale in the pet store")
@JacksonXmlRootElement(localName = "Pet")
@XmlRootElement(name = "Pet")
@XmlAccessorType(XmlAccessType.FIELD)

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Pet {

Expand Down Expand Up @@ -104,6 +111,7 @@ public Pet id(Long id) {

@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("id")
@JacksonXmlProperty(localName = "id")
public Long getId() {
return id;
}
Expand All @@ -124,6 +132,7 @@ public Pet category(Category category) {
@Valid
@Schema(name = "category", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("category")
@JacksonXmlProperty(localName = "Category")
public Category getCategory() {
return category;
}
Expand All @@ -144,6 +153,7 @@ public Pet name(String name) {
@NotNull
@Schema(name = "name", example = "doggie", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("name")
@JacksonXmlProperty(localName = "name")
public String getName() {
return name;
}
Expand Down Expand Up @@ -172,6 +182,7 @@ public Pet addPhotoUrlsItem(String photoUrlsItem) {
@NotNull
@Schema(name = "photoUrls", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("photoUrls")
@JacksonXmlProperty(localName = "photoUrl")
public List<String> getPhotoUrls() {
return photoUrls;
}
Expand Down Expand Up @@ -200,6 +211,7 @@ public Pet addTagsItem(Tag tagsItem) {
@Valid
@Schema(name = "tags", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("tags")
@JacksonXmlProperty(localName = "tag")
public List<@Valid Tag> getTags() {
return tags;
}
Expand All @@ -221,6 +233,7 @@ public Pet status(StatusEnum status) {

@Schema(name = "status", description = "pet status in the store", deprecated = true, requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("status")
@JacksonXmlProperty(localName = "status")
@Deprecated
public StatusEnum getStatus() {
return status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import jakarta.xml.bind.annotation.*;

import java.util.*;
import jakarta.annotation.Generated;
Expand All @@ -19,6 +22,10 @@
*/

@Schema(name = "Tag", description = "A tag for a pet")
@JacksonXmlRootElement(localName = "Tag")
@XmlRootElement(name = "Tag")
@XmlAccessorType(XmlAccessType.FIELD)

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Tag {

Expand All @@ -38,6 +45,7 @@ public Tag id(Long id) {

@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("id")
@JacksonXmlProperty(localName = "id")
public Long getId() {
return id;
}
Expand All @@ -58,6 +66,7 @@ public Tag name(String name) {

@Schema(name = "name", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("name")
@JacksonXmlProperty(localName = "name")
public String getName() {
return name;
}
Expand Down
Loading
Loading