Skip to content

Commit c43eef3

Browse files
committed
Add bean validation annotations for the method parameters which are treated as headers
1 parent 8d7be74 commit c43eef3

File tree

16 files changed

+465
-1
lines changed

16 files changed

+465
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
generatorName: spring
2+
outputDir: samples/server/petstore/spring-mvc-headers-validation
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/headers-validation.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
5+
additionalProperties:
6+
java8: true
7+
performBeanValidation: "true"
8+
hideGenerationTimestamp: "true"
9+
artifactId: spring-mvc-headers-validation
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isHeaderParam}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{^isContainer}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}{{/isContainer}}) @RequestHeader(value = "{{baseName}}", required = {{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
1+
{{#isHeaderParam}}{{#useBeanValidation}}{{>beanValidationCore}}{{/useBeanValidation}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{^isContainer}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}{{/isContainer}}) @RequestHeader(value = "{{baseName}}", required = {{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test for Issue 7125
4+
description: desc
5+
version: 1.0.0
6+
tags:
7+
- name: verify-headers-validation
8+
description: verify headers validation
9+
paths:
10+
/test-headers-validation:
11+
get:
12+
tags:
13+
- verify-headers-validation
14+
summary: test headers validation
15+
description: desc
16+
operationId: headersValidationTest
17+
parameters:
18+
- name: stringWithMinLength
19+
in: header
20+
schema:
21+
type: string
22+
minLength: 32
23+
- name: stringWithMaxLength
24+
in: header
25+
schema:
26+
type: string
27+
maxLength: 32
28+
- name: stringWithLength
29+
in: header
30+
schema:
31+
type: string
32+
minLength: 32
33+
maxLength: 32
34+
- name: stringWithPattern
35+
in: header
36+
schema:
37+
type: string
38+
pattern: ^([0-9a-fA-F]{32})$
39+
responses:
40+
200:
41+
description: default response
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
README.md
2+
pom.xml
3+
src/main/java/org/openapitools/OpenAPI2SpringBoot.java
4+
src/main/java/org/openapitools/RFC3339DateFormat.java
5+
src/main/java/org/openapitools/api/ApiUtil.java
6+
src/main/java/org/openapitools/api/TestHeadersValidationApi.java
7+
src/main/java/org/openapitools/api/TestHeadersValidationApiController.java
8+
src/main/java/org/openapitools/configuration/HomeController.java
9+
src/main/java/org/openapitools/configuration/OpenAPIDocumentationConfig.java
10+
src/main/resources/application.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5.3.0-SNAPSHOT
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# OpenAPI generated server
2+
3+
Spring Boot Server
4+
5+
6+
## Overview
7+
This server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
8+
By using the [OpenAPI-Spec](https://openapis.org), you can easily generate a server stub.
9+
This is an example of building a OpenAPI-enabled server in Java using the SpringBoot framework.
10+
11+
The underlying library integrating OpenAPI to SpringBoot is [springfox](https://github.com/springfox/springfox)
12+
13+
Start your server as a simple java application
14+
15+
You can view the api documentation in swagger-ui by pointing to
16+
http://localhost:8080/
17+
18+
Change default port value in application.properties
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.openapitools</groupId>
4+
<artifactId>spring-mvc-headers-validation</artifactId>
5+
<packaging>jar</packaging>
6+
<name>spring-mvc-headers-validation</name>
7+
<version>1.0.0</version>
8+
<properties>
9+
<java.version>1.8</java.version>
10+
<maven.compiler.source>${java.version}</maven.compiler.source>
11+
<maven.compiler.target>${java.version}</maven.compiler.target>
12+
<springfox-version>2.9.2</springfox-version>
13+
</properties>
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.3.3.RELEASE</version>
18+
</parent>
19+
<build>
20+
<sourceDirectory>src/main/java</sourceDirectory>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-maven-plugin</artifactId>
25+
<executions>
26+
<execution>
27+
<goals>
28+
<goal>repackage</goal>
29+
</goals>
30+
</execution>
31+
</executions>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.data</groupId>
42+
<artifactId>spring-data-commons</artifactId>
43+
</dependency>
44+
<!--SpringFox dependencies -->
45+
<dependency>
46+
<groupId>io.springfox</groupId>
47+
<artifactId>springfox-swagger2</artifactId>
48+
<version>${springfox-version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.springfox</groupId>
52+
<artifactId>springfox-swagger-ui</artifactId>
53+
<version>${springfox-version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>jakarta.xml.bind</groupId>
57+
<artifactId>jakarta.xml.bind-api</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.fasterxml.jackson.datatype</groupId>
61+
<artifactId>jackson-datatype-jsr310</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.openapitools</groupId>
65+
<artifactId>jackson-databind-nullable</artifactId>
66+
<version>0.2.1</version>
67+
</dependency>
68+
<!-- Bean Validation API support -->
69+
<dependency>
70+
<groupId>jakarta.validation</groupId>
71+
<artifactId>jakarta.validation-api</artifactId>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.fasterxml.jackson.core</groupId>
75+
<artifactId>jackson-databind</artifactId>
76+
</dependency>
77+
</dependencies>
78+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.openapitools;
2+
3+
import com.fasterxml.jackson.databind.Module;
4+
import org.openapitools.jackson.nullable.JsonNullableModule;
5+
import org.springframework.boot.CommandLineRunner;
6+
import org.springframework.boot.ExitCodeGenerator;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.ComponentScan;
11+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
12+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
13+
14+
@SpringBootApplication
15+
@ComponentScan(basePackages = {"org.openapitools", "org.openapitools.api" , "org.openapitools.configuration"})
16+
public class OpenAPI2SpringBoot implements CommandLineRunner {
17+
18+
@Override
19+
public void run(String... arg0) throws Exception {
20+
if (arg0.length > 0 && arg0[0].equals("exitcode")) {
21+
throw new ExitException();
22+
}
23+
}
24+
25+
public static void main(String[] args) throws Exception {
26+
new SpringApplication(OpenAPI2SpringBoot.class).run(args);
27+
}
28+
29+
static class ExitException extends RuntimeException implements ExitCodeGenerator {
30+
private static final long serialVersionUID = 1L;
31+
32+
@Override
33+
public int getExitCode() {
34+
return 10;
35+
}
36+
37+
}
38+
39+
@Bean
40+
public WebMvcConfigurer webConfigurer() {
41+
return new WebMvcConfigurer() {
42+
/*@Override
43+
public void addCorsMappings(CorsRegistry registry) {
44+
registry.addMapping("/**")
45+
.allowedOrigins("*")
46+
.allowedMethods("*")
47+
.allowedHeaders("Content-Type");
48+
}*/
49+
};
50+
}
51+
52+
@Bean
53+
public Module jsonNullableModule() {
54+
return new JsonNullableModule();
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.openapitools;
2+
3+
import com.fasterxml.jackson.databind.util.StdDateFormat;
4+
5+
import java.text.DateFormat;
6+
import java.text.FieldPosition;
7+
import java.text.ParsePosition;
8+
import java.util.Date;
9+
import java.util.GregorianCalendar;
10+
import java.util.TimeZone;
11+
12+
public class RFC3339DateFormat extends DateFormat {
13+
private static final long serialVersionUID = 1L;
14+
private static final TimeZone TIMEZONE_Z = TimeZone.getTimeZone("UTC");
15+
16+
private final StdDateFormat fmt = new StdDateFormat()
17+
.withTimeZone(TIMEZONE_Z)
18+
.withColonInTimeZone(true);
19+
20+
public RFC3339DateFormat() {
21+
this.calendar = new GregorianCalendar();
22+
}
23+
24+
@Override
25+
public Date parse(String source, ParsePosition pos) {
26+
return fmt.parse(source, pos);
27+
}
28+
29+
@Override
30+
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
31+
return fmt.format(date, toAppendTo, fieldPosition);
32+
}
33+
34+
@Override
35+
public Object clone() {
36+
return this;
37+
}
38+
}

0 commit comments

Comments
 (0)