Skip to content
Draft
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 @@ -662,9 +662,10 @@ public void processOpts() {
typeMapping.put("date", "LocalDate");
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("LocalTime", "java.time.LocalTime");
typeMapping.put("date-time-local", "LocalDateTime");
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
if ("java8-localdatetime".equals(dateLibrary)) {
typeMapping.put("DateTime", "LocalDateTime");
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
} else {
typeMapping.put("DateTime", "OffsetDateTime");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,35 @@ public void generateFormatForDateAndDateTimeQueryParam() throws IOException {
.containsWithNameAndAttributes("DateTimeFormat", ImmutableMap.of("iso", "DateTimeFormat.ISO.DATE_TIME"));
}

@Test
public void generateLocalDateTimeForDateTimeLocalFormat() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
generator.setGenerateMetadata(false);
generator.opts(input).generate();

JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Pet.java"))
.hasImports("java.time.LocalDateTime")
.assertProperty("adoptionDate").withType("LocalDateTime");
}

@Test
public void interfaceDefaultImplDisableWithResponseWrapper() {
final SpringCodegen codegen = new SpringCodegen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ components:
type: string
format: date
default: '2021-01-01'
adoptionDate:
type: string
format: date-time-local
default: '2007-12-03T10:15:30'
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -40,6 +41,8 @@ public class Pet {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateOfBirth = LocalDate.parse("2021-01-01");

private LocalDateTime adoptionDate = "2007-12-03T10:15:30";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks off?


public Pet() {
super();
}
Expand Down Expand Up @@ -177,6 +180,27 @@ public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public Pet adoptionDate(LocalDateTime adoptionDate) {
this.adoptionDate = adoptionDate;
return this;
}

/**
* Get adoptionDate
* @return adoptionDate
*/
@Valid
@Schema(name = "adoptionDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("adoptionDate")
public LocalDateTime getAdoptionDate() {
return adoptionDate;
}

@JsonProperty("adoptionDate")
public void setAdoptionDate(LocalDateTime adoptionDate) {
this.adoptionDate = adoptionDate;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -191,12 +215,13 @@ public boolean equals(Object o) {
Objects.equals(this.happy, pet.happy) &&
Objects.equals(this.price, pet.price) &&
Objects.equals(this.lastFeed, pet.lastFeed) &&
Objects.equals(this.dateOfBirth, pet.dateOfBirth);
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
Objects.equals(this.adoptionDate, pet.adoptionDate);
}

@Override
public int hashCode() {
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth);
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, adoptionDate);
}

@Override
Expand All @@ -209,6 +234,7 @@ public String toString() {
sb.append(" price: ").append(toIndentedString(price)).append("\n");
sb.append(" lastFeed: ").append(toIndentedString(lastFeed)).append("\n");
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -40,6 +41,8 @@ public class Pet {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateOfBirth = LocalDate.parse("2021-01-01");

private LocalDateTime adoptionDate = "2007-12-03T10:15:30";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


public Pet() {
super();
}
Expand Down Expand Up @@ -177,6 +180,27 @@ public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public Pet adoptionDate(LocalDateTime adoptionDate) {
this.adoptionDate = adoptionDate;
return this;
}

/**
* Get adoptionDate
* @return adoptionDate
*/
@Valid
@Schema(name = "adoptionDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("adoptionDate")
public LocalDateTime getAdoptionDate() {
return adoptionDate;
}

@JsonProperty("adoptionDate")
public void setAdoptionDate(LocalDateTime adoptionDate) {
this.adoptionDate = adoptionDate;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -191,12 +215,13 @@ public boolean equals(Object o) {
Objects.equals(this.happy, pet.happy) &&
Objects.equals(this.price, pet.price) &&
Objects.equals(this.lastFeed, pet.lastFeed) &&
Objects.equals(this.dateOfBirth, pet.dateOfBirth);
Objects.equals(this.dateOfBirth, pet.dateOfBirth) &&
Objects.equals(this.adoptionDate, pet.adoptionDate);
}

@Override
public int hashCode() {
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth);
return Objects.hash(atType, age, happy, price, lastFeed, dateOfBirth, adoptionDate);
}

@Override
Expand All @@ -209,6 +234,7 @@ public String toString() {
sb.append(" price: ").append(toIndentedString(price)).append("\n");
sb.append(" lastFeed: ").append(toIndentedString(lastFeed)).append("\n");
sb.append(" dateOfBirth: ").append(toIndentedString(dateOfBirth)).append("\n");
sb.append(" adoptionDate: ").append(toIndentedString(adoptionDate)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Basic polymorphism example without discriminator
* Polymorphism example with allOf and discriminator
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
Expand All @@ -11,21 +11,24 @@
*/
package org.openapitools.server.models

import org.openapitools.server.models.Pet

/**
* A pet cat
* A representation of a cat
* @param huntingSkill The measured skill for hunting
* @param petType
*/
data class Cat(
/* The measured skill for hunting */

@field:com.fasterxml.jackson.annotation.JsonProperty("huntingSkill")
val huntingSkill: Cat.HuntingSkill,

@field:com.fasterxml.jackson.annotation.JsonProperty("name")
override val name: kotlin.String,

@field:com.fasterxml.jackson.annotation.JsonProperty("petType")
val petType: kotlin.Any? = null
)
override val petType: kotlin.String
) : Pet(name = name, petType = petType)
{
/**
* The measured skill for hunting
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Basic polymorphism example without discriminator
* Polymorphism example with allOf and discriminator
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
Expand All @@ -11,19 +11,24 @@
*/
package org.openapitools.server.models

import org.openapitools.server.models.Pet

/**
* A pet dog
* A representation of a dog
* @param packSize the size of the pack the dog is from
* @param petType
*/
data class Dog(
/* the size of the pack the dog is from */

@field:com.fasterxml.jackson.annotation.JsonProperty("packSize")
val packSize: kotlin.Int = 0,

@field:com.fasterxml.jackson.annotation.JsonProperty("name")
override val name: kotlin.String,

@field:com.fasterxml.jackson.annotation.JsonProperty("petType")
val petType: kotlin.Any? = null
)
override val petType: kotlin.String
) : Pet(name = name, petType = petType)
{
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Basic polymorphism example without discriminator
* Polymorphism example with allOf and discriminator
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
Expand All @@ -11,42 +11,25 @@
*/
package org.openapitools.server.models

import org.openapitools.server.models.Cat
import org.openapitools.server.models.Dog

/**
*
* @param name
* @param petType
* @param huntingSkill The measured skill for hunting
* @param packSize the size of the pack the dog is from
*/
data class Pet(
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "petType", visible = true)
@com.fasterxml.jackson.annotation.JsonSubTypes(
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Cat::class, name = "Cat"),
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Dog::class, name = "Dog")
)
sealed class Pet(

@field:com.fasterxml.jackson.annotation.JsonProperty("name")
val name: kotlin.String,
open val name: kotlin.String
,

@field:com.fasterxml.jackson.annotation.JsonProperty("petType")
val petType: kotlin.Any?,
/* The measured skill for hunting */

@field:com.fasterxml.jackson.annotation.JsonProperty("huntingSkill")
val huntingSkill: Pet.HuntingSkill,
/* the size of the pack the dog is from */

@field:com.fasterxml.jackson.annotation.JsonProperty("packSize")
val packSize: kotlin.Int = 0
open val petType: kotlin.String

)
{
/**
* The measured skill for hunting
* Values: clueless,lazy,adventurous,aggressive
*/
enum class HuntingSkill(val value: kotlin.String){
clueless("clueless"),
lazy("lazy"),
adventurous("adventurous"),
aggressive("aggressive");
}
}