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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/isContainer}}
{{/isEnum}}
{{#jackson}}
@JsonProperty("{{baseName}}")
{{#withXml}}
Comment thread
ceremo marked this conversation as resolved.
@JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{.}}", {{/xmlNamespace}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
{{/withXml}}
Expand Down Expand Up @@ -185,6 +184,9 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{#swagger1AnnotationLibrary}}
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}")
{{/swagger1AnnotationLibrary}}
{{#jackson}}
@JsonProperty("{{baseName}}")
{{/jackson}}
public {{>nullableDataType}} {{getter}}() {
return {{name}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public ACTUAL containsWithName(final String name) {
return myself();
}

public ACTUAL doesNotContainsWithName(final String name) {
super
.withFailMessage("Should have annotation with name: " + name)
.noneMatch(annotation -> annotation.getNameAsString().equals(name));
return myself();
}

public ACTUAL containsWithNameAndAttributes(final String name, final Map<String, String> attributes) {
super
.withFailMessage("Should have annotation with name: " + name + " and attributes: " + attributes + ", but was: " + actual)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2144,4 +2144,57 @@ private Map<String, File> generateFromContract(String url, String library, Map<S
return generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
}

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

OpenAPI openAPI = new OpenAPIParser()
.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.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()));

JavaFileAssert.assertThat(files.get("ResponseObjectWithDifferentFieldNames.java"))
.hasProperty("normalPropertyName")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.toProperty().toType()
.hasProperty("UPPER_CASE_PROPERTY_SNAKE")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.toProperty().toType()
.hasProperty("lowerCasePropertyDashes")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.toProperty().toType()
.hasProperty("propertyNameWithSpaces")
.assertPropertyAnnotations()
.doesNotContainsWithName("JsonProperty")
.toProperty().toType()
.assertMethod("getNormalPropertyName")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"normalPropertyName\""))
.toMethod().toFileAssert()
.assertMethod("getUPPERCASEPROPERTYSNAKE")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"UPPER_CASE_PROPERTY_SNAKE\""))
.toMethod().toFileAssert()
.assertMethod("getLowerCasePropertyDashes")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"lower-case-property-dashes\""))
.toMethod().toFileAssert()
.assertMethod("getPropertyNameWithSpaces")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonProperty", ImmutableMap.of("value", "\"property name with spaces\""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,26 @@ paths:
- petstore_auth:
- write:pets
- read:pets
/fake/{petId}/response-object-different-names:
get:
tags:
- pet
operationId: responseObjectDifferentNames
parameters:
- name: petId
in: path
description: ID of pet to update
required: true
schema:
type: integer
format: int64
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: "#/components/schemas/ResponseObjectWithDifferentFieldNames"
servers:
- url: http://petstore.swagger.io:80/v2
components:
Expand Down Expand Up @@ -2006,3 +2026,14 @@ components:
items:
type: string
default: ["foo", "bar"]
ResponseObjectWithDifferentFieldNames:
type: object
properties:
normalPropertyName:
type: string
UPPER_CASE_PROPERTY_SNAKE:
type: string
lower-case-property-dashes:
type: string
property name with spaces:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,17 @@
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Pet {

@JsonProperty("@type")
private String atType = "Pet";

@JsonProperty("age")
private Integer age = 4;

@JsonProperty("happy")
private Boolean happy = true;

@JsonProperty("price")
private BigDecimal price = new BigDecimal("32000000000");

@JsonProperty("lastFeed")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime lastFeed = OffsetDateTime.parse("1973-12-19T11:39:57Z[UTC]", java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault()));

@JsonProperty("dateOfBirth")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateOfBirth = LocalDate.parse("2021-01-01");

Expand Down Expand Up @@ -73,6 +67,7 @@ public Pet atType(String atType) {
*/
@NotNull
@ApiModelProperty(required = true, value = "")
@JsonProperty("@type")
public String getAtType() {
return atType;
}
Expand All @@ -92,6 +87,7 @@ public Pet age(Integer age) {
*/

@ApiModelProperty(value = "")
@JsonProperty("age")
public Integer getAge() {
return age;
}
Expand All @@ -111,6 +107,7 @@ public Pet happy(Boolean happy) {
*/

@ApiModelProperty(value = "")
@JsonProperty("happy")
public Boolean getHappy() {
return happy;
}
Expand All @@ -130,6 +127,7 @@ public Pet price(BigDecimal price) {
*/
@Valid
@ApiModelProperty(value = "")
@JsonProperty("price")
public BigDecimal getPrice() {
return price;
}
Expand All @@ -149,6 +147,7 @@ public Pet lastFeed(OffsetDateTime lastFeed) {
*/
@Valid
@ApiModelProperty(value = "")
@JsonProperty("lastFeed")
public OffsetDateTime getLastFeed() {
return lastFeed;
}
Expand All @@ -168,6 +167,7 @@ public Pet dateOfBirth(LocalDate dateOfBirth) {
*/
@Valid
@ApiModelProperty(value = "")
@JsonProperty("dateOfBirth")
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Category {

@JsonProperty("id")
private Long id;

@JsonProperty("name")
private String name;

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

@ApiModelProperty(value = "")
@JsonProperty("id")
public Long getId() {
return id;
}
Expand All @@ -59,6 +58,7 @@ public Category name(String name) {
*/
@Pattern(regexp = "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")
@ApiModelProperty(value = "")
@JsonProperty("name")
public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class ModelApiResponse {

@JsonProperty("code")
private Integer code;

@JsonProperty("type")
private String type;

@JsonProperty("message")
private String message;

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

@ApiModelProperty(value = "")
@JsonProperty("code")
public Integer getCode() {
return code;
}
Expand All @@ -64,6 +62,7 @@ public ModelApiResponse type(String type) {
*/

@ApiModelProperty(value = "")
@JsonProperty("type")
public String getType() {
return type;
}
Expand All @@ -83,6 +82,7 @@ public ModelApiResponse message(String message) {
*/

@ApiModelProperty(value = "")
@JsonProperty("message")
public String getMessage() {
return message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Order {

@JsonProperty("id")
private Long id;

@JsonProperty("petId")
private Long petId;

@JsonProperty("quantity")
private Integer quantity;

@JsonProperty("shipDate")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime shipDate;

Expand Down Expand Up @@ -76,10 +72,8 @@ public static StatusEnum fromValue(String value) {
}
}

@JsonProperty("status")
private StatusEnum status;

@JsonProperty("complete")
private Boolean complete = false;

public Order id(Long id) {
Expand All @@ -93,6 +87,7 @@ public Order id(Long id) {
*/

@ApiModelProperty(value = "")
@JsonProperty("id")
public Long getId() {
return id;
}
Expand All @@ -112,6 +107,7 @@ public Order petId(Long petId) {
*/

@ApiModelProperty(value = "")
@JsonProperty("petId")
public Long getPetId() {
return petId;
}
Expand All @@ -131,6 +127,7 @@ public Order quantity(Integer quantity) {
*/

@ApiModelProperty(value = "")
@JsonProperty("quantity")
public Integer getQuantity() {
return quantity;
}
Expand All @@ -150,6 +147,7 @@ public Order shipDate(OffsetDateTime shipDate) {
*/
@Valid
@ApiModelProperty(value = "")
@JsonProperty("shipDate")
public OffsetDateTime getShipDate() {
return shipDate;
}
Expand All @@ -169,6 +167,7 @@ public Order status(StatusEnum status) {
*/

@ApiModelProperty(value = "Order Status")
@JsonProperty("status")
public StatusEnum getStatus() {
return status;
}
Expand All @@ -188,6 +187,7 @@ public Order complete(Boolean complete) {
*/

@ApiModelProperty(value = "")
@JsonProperty("complete")
public Boolean getComplete() {
return complete;
}
Expand Down
Loading