BAEL-5288 Code for the Map Date Types With openapi generator article (#13467)

Co-authored-by: thibault.faure <thibault.faure@mimacom.com>
This commit is contained in:
thibaultfaure 2023-02-17 02:16:55 +01:00 committed by GitHub
parent db11ca931d
commit 8890de875d
3 changed files with 115 additions and 0 deletions

View File

@ -20,6 +20,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springdoc</groupId> <groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId> <artifactId>springdoc-openapi-ui</artifactId>
@ -59,6 +63,7 @@
<version>${swagger-codegen-maven-plugin.version}</version> <version>${swagger-codegen-maven-plugin.version}</version>
<executions> <executions>
<execution> <execution>
<id>two-responses</id>
<goals> <goals>
<goal>generate</goal> <goal>generate</goal>
</goals> </goals>
@ -71,6 +76,59 @@
</configOptions> </configOptions>
</configuration> </configuration>
</execution> </execution>
<execution>
<id>dates</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/static/event.yaml</inputSpec>
<language>spring</language>
<configOptions>
<java8>true</java8>
<dateLibrary>custom</dateLibrary>
</configOptions>
<typeMappings>
<typeMapping>DateTime=Instant</typeMapping>
<typeMapping>Date=Date</typeMapping>
</typeMappings>
<importMappings>
<importMapping>Instant=java.time.Instant</importMapping>
<importMapping>Date=java.util.Date</importMapping>
</importMappings>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<skipValidateSpec>true</skipValidateSpec>
<inputSpec>${project.basedir}/src/main/resources/static/event.yaml</inputSpec>
<generatorName>spring</generatorName>
<configOptions>
<java8>true</java8>
<dateLibrary>custom</dateLibrary>
<openApiNullable>false</openApiNullable>
<interfaceOnly>true</interfaceOnly>
</configOptions>
<typeMappings>
<typeMapping>DateTime=Instant</typeMapping>
<typeMapping>Date=Date</typeMapping>
</typeMappings>
<importMappings>
<importMapping>Instant=java.time.Instant</importMapping>
<importMapping>Date=java.util.Date</importMapping>
</importMappings>
</configuration>
</execution>
</executions> </executions>
</plugin> </plugin>
<plugin> <plugin>
@ -84,6 +142,7 @@
</build> </build>
<properties> <properties>
<openapi-generator.version>6.2.1</openapi-generator.version>
<springfox.version>3.0.0</springfox.version> <springfox.version>3.0.0</springfox.version>
<swagger-codegen-maven-plugin.version>3.0.34</swagger-codegen-maven-plugin.version> <swagger-codegen-maven-plugin.version>3.0.34</swagger-codegen-maven-plugin.version>
<springdoc.version>1.6.10</springdoc.version> <springdoc.version>1.6.10</springdoc.version>

View File

@ -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}"

View File

@ -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<ConstraintViolation<Event>> violations = VALIDATOR.validate(new Event().ticketSales("01-01-2024"));
assertTrue(violations.isEmpty());
}
@Test
void givenAWronglyFormattedTicketSales_WhenBuildingEvent_ThenSuccess() {
Set<ConstraintViolation<Event>> violations = VALIDATOR.validate(new Event().ticketSales("2024-01-01"));
assertEquals(1, violations.size());
}
}