From 8890de875da89e0c252e23ff3f1c2124f6cdcac3 Mon Sep 17 00:00:00 2001 From: thibaultfaure Date: Fri, 17 Feb 2023 02:16:55 +0100 Subject: [PATCH] BAEL-5288 Code for the Map Date Types With openapi generator article (#13467) Co-authored-by: thibault.faure --- .../spring-boot-swagger-2/pom.xml | 59 +++++++++++++++++++ .../src/main/resources/static/event.yaml | 23 ++++++++ .../com/baeldung/dates/EventUnitTest.java | 33 +++++++++++ 3 files changed, 115 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/event.yaml create mode 100644 spring-boot-modules/spring-boot-swagger-2/src/test/java/com/baeldung/dates/EventUnitTest.java diff --git a/spring-boot-modules/spring-boot-swagger-2/pom.xml b/spring-boot-modules/spring-boot-swagger-2/pom.xml index 1cd8e5b850..d2d1d10ad9 100644 --- a/spring-boot-modules/spring-boot-swagger-2/pom.xml +++ b/spring-boot-modules/spring-boot-swagger-2/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springdoc springdoc-openapi-ui @@ -59,6 +63,7 @@ ${swagger-codegen-maven-plugin.version} + two-responses generate @@ -71,6 +76,59 @@ + + dates + + generate + + + ${project.basedir}/src/main/resources/static/event.yaml + spring + + true + custom + + + DateTime=Instant + Date=Date + + + Instant=java.time.Instant + Date=java.util.Date + + + + + + + org.openapitools + openapi-generator-maven-plugin + ${openapi-generator.version} + + + + generate + + + true + ${project.basedir}/src/main/resources/static/event.yaml + spring + + true + custom + false + true + + + DateTime=Instant + Date=Date + + + Instant=java.time.Instant + Date=java.util.Date + + + @@ -84,6 +142,7 @@ + 6.2.1 3.0.0 3.0.34 1.6.10 diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/event.yaml b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/event.yaml new file mode 100644 index 0000000000..f8a7b01b43 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/event.yaml @@ -0,0 +1,23 @@ +openapi: 3.0.0 +info: + title: an example api with dates + version: 0.1.0 +paths: +components: + schemas: + Event: + type: object + properties: + organizer: + type: string + startDate: + type: string + format: date + endDate: + type: string + format: date-time + ticketSales: + type: string + description: Beginning of the ticket sales + example: "01-01-2023" + pattern: "[0-9]{2}-[0-9]{2}-[0-9]{4}" \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger-2/src/test/java/com/baeldung/dates/EventUnitTest.java b/spring-boot-modules/spring-boot-swagger-2/src/test/java/com/baeldung/dates/EventUnitTest.java new file mode 100644 index 0000000000..378882c964 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/test/java/com/baeldung/dates/EventUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.dates; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; + +import org.junit.jupiter.api.Test; + +import io.swagger.model.Event; + +class EventUnitTest { + + private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory() + .getValidator(); + + @Test + void givenACorrectlyFormattedTicketSales_WhenBuildingEvent_ThenSuccess() { + Set> violations = VALIDATOR.validate(new Event().ticketSales("01-01-2024")); + assertTrue(violations.isEmpty()); + } + + @Test + void givenAWronglyFormattedTicketSales_WhenBuildingEvent_ThenSuccess() { + Set> violations = VALIDATOR.validate(new Event().ticketSales("2024-01-01")); + assertEquals(1, violations.size()); + } + +}