SpringBoot validation doesn't work with openapi - spring-boot

I'm using openapi-generator-maven-plugin version 5.4.0 to generate classes but the validaiton doesn't seem to be working in my springboot application. When I test the below with a missing name, I'm still able to make a request to the controller without any validation error. How can I fix it.
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.4.0</version>
<executions>
<execution>
<id>customer</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatorName>java</generatorName>
<library>resttemplate</library>
<inputSpec>${project.basedir}/doc/swagger.yml</inputSpec>
<modelPackage>com.test.model</modelPackage>
<generateModels>true</generateModels>
<generateApis>false</generateApis>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateApiDocumentation>false</generateApiDocumentation>
<generateSupportingFiles>false</generateSupportingFiles>
<addCompileSourceRoot>true</addCompileSourceRoot>
<skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
<strict>true</strict>
<configOptions>
<sourceFolder>models</sourceFolder>
<dateLibrary>java11</dateLibrary>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
UserRequest:
type: object
required:
- address
- name
properties:
name:
$ref: '#/components/schemas/Address'
surname:
$ref: '#/components/schemas/Name'
Address:
type: string
description: "First name of the customer"
Name:
type: string
description: "Last name/Surname of the customer"
#PostMapping("user")
public ResponseEntity<UserResponse> createCustomer(#RequestBody UserRequest userRequest) {
return new ResponseEntity<>(userServicer.newUser(userRequest), HttpStatus.OK);
}

Related

ProGuard Maven Can't find common super class

I just can't seem to get this working:
[proguard] Class = [co/aikar/commands/lib/expiringmap/ExpiringMap]
[proguard] Method = [<init>(Lco/aikar/commands/lib/expiringmap/ExpiringMap$Builder;)V]
[proguard] Exception = [proguard.evaluation.IncompleteClassHierarchyException] (Can't find common super class of
[co.aikar.commands.lib.expiringmap.ExpiringMap$EntryLinkedHashMap] (with 1 known super classes: co.aikar.commands.lib.expiringmap.ExpiringMap$EntryLinkedHashMap) and
[co.aikar.commands.lib.expiringmap.ExpiringMap$EntryTreeHashMap] (with 1 known super classes: co.aikar.commands.lib.expiringmap.ExpiringMap$EntryTreeHashMap))
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>proguard</goal></goals>
<configuration>
<options>
<option>-keep class co.aikar.** { *; }</option>
<option>-dontnote</option>
<option>-dontshrink</option>
<option>-dontoptimize</option>
<option>-dontwarn</option>
<option>-allowaccessmodification</option>
</options>
</configuration>
</execution>
</executions>
</plugin>
Those classes are shaded in by the maven-shade-plugin but somehow keep throwing errors. The jar containing those classes are also passed with the -injars option.

springdoc-openapi-maven-plugin adding example at null in YAML file

I'm using springdoc-openapi-maven-plugin to generate a contract in YAML format and somehow it generates an example at null (example: null) for each path/request parameter.
Is there a way to avoid that ?
Here is an example of the generated YAML
openapi: 3.0.1
paths:
/myapi/v1/resource/{id}:
get:
parameters:
- name: id
in: path
required: true
schema:
type: string
example: null
- name: param1
in: query
required: true
schema:
type: string
example: null
And the plugin (pom.xml)
<plugin>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<apiDocsUrl>http://localhost:8080/v3/api-docs.yaml</apiDocsUrl>
<outputFileName>myYamlFile.yaml</outputFileName>
<outputDir>/home/</outputDir>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
The ResourceController (very basic) :
#RestController
#RequestMapping("/myapi/v1/resource")
public class ResourceController {
#GetMapping("/{id}")
public ResourceDTO getResourceInfo(#PathVariable("id") String resourceId, #RequestParam(value="param1") String param1) {
[...]
}
}
EDIT - The apparition of the example: null started with the upgrade of the jackson-databind dependency to 2.14.0
For me it was using an older version of springdoc-openapu-webflux-ui.
Changed it to version 1.6.11 from 1.6.5 and it fixed it.

openapi codegen-maven-plugin eliminate suffix UsingGET in generated methods

I'm trying to generate interfaces from yaml file with openapi codegen-maven-plugin every thing is good except the generated methods having the suffix UsingGET as you can see in this exemple bellow :
ResponseEntity<ApicatControl> retrieveRepeatedProductOfferingUsingGET(
#Parameter(name = "category.id", description = "category.id", schema = #Schema(description = "")) #Valid #RequestParam(value = "category.id", required = false) String categoryId,
#Parameter(name = "type", description = "type", schema = #Schema(description = "")) #Valid #RequestParam(value = "type", required = false) String type
);
And this is my configuration for codegen-maven-plugin within pom.xml.
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.4.0</version>
<executions>
<execution>
<id>openapi-codegen-java-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger/swagger.yaml</inputSpec>
<generatorName>spring</generatorName>
<generateApiTests>false</generateApiTests>
<modelPackage>com.groupe.apicat.gu.api.resources.model</modelPackage>
<apiPackage>com.groupe.apicat.gu.api</apiPackage>
<output>${generated-sources-path}</output>
<templateDirectory>src/templates/service</templateDirectory>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModels>true</generateModels>
<configOptions>
<skipDefaultInterface>true</skipDefaultInterface>
<interfaceOnly>true</interfaceOnly>
<sourceFolder>generated-sources</sourceFolder>
<dateLibrary>legacy</dateLibrary>
<returnResponse>true</returnResponse>
<library>spring-boot</library>
<useTags>true</useTags>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<useSwaggerAnnotations>true</useSwaggerAnnotations>
<serializableModel>true</serializableModel>
<delegatePattern>false</delegatePattern>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Please do you have any solutions!! thanks
The method name "retrieveRepeatedProductOfferingUsingGET" is created within your swagger.yaml. There should be a parameter called "operationId" where you specify this name. So just change it there and build the project again.

maven failsafe plugin with Junit 5 - cannot run filtered groups using command line and Junit 5 #Tag

I am trying to run failsafe plugin for integration tests using Junit 5 tags. My POM.xml for failsafe looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.version}</version>
<configuration>
<systemProperties>
<phantomjs.binary.path>${phantomjs.binary.path}</phantomjs.binary.path>
<webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver>
<webdriver.ie.driver>${webdriver.ie.driver}</webdriver.ie.driver>
<webdriver.edge.driver>${webdriver.edge.driver}</webdriver.edge.driver>
<webdriver.gecko.driver>${webdriver.gecko.driver}</webdriver.gecko.driver>
<webdriver.opera.driver>${webdriver.opera.driver}</webdriver.opera.driver>
<selenium.wait.timeout>30</selenium.wait.timeout>
</systemProperties>
<configuration>
<groups>EveryDay|Today</groups>
<excludedGroups>integration, regression</excludedGroups>
</configuration>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
and trying to do:
mvn -Dgroups=Today verify
It does not work and run the whole suite. Any ideas?
My test method looks like:
#Test
#Tag("EveryDay")
#Tag("Today")
#DisplayName("Activities")
public void activitiesTest(){ // Some test code here }
and my test class:
#ExtendWith({SpringExtension.class})
#ContextConfiguration(classes = { WebDriverConfig.class, LoggerConfig.class, EmailConfig.class})
#TestExecutionListeners(listeners= {ScreenshotTaker.class, DependencyInjectionTestExecutionListener.class, RunnerExtension.class})
public class BasicScenariosIT {
// Code
}
Actually the solution was quite simple...
In my maven pom.xml, in the failsafe plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.version}</version>
<configuration>
<groups>${test.included.groups}</groups>
<excludedGroups>${test.excluded.groups}</excludedGroups>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Note the parameters:
${test.included.groups}
${test.excluded.groups}
In my junit 5 test:
#Test
#Tag("EveryDay")
#Tag("Today")
#DisplayName("Activities")
public void activitiesTest(){ // Some test code here }
and the command:
mvn -Dtest.included.groups=Today verify
That's it!!

How to define #Parameter annotation in Maven POM

I wrote a Mojo Plugin and set two #Parameter (import org.apache.maven.plugins.annotations.Parameter;)
I want to configure the Parameters in the POM of the project where I want to use this plugin.
No matter where everytime I get an error message.
The part of the POM:
<plugin>
<groupId>com.tup.test</groupId>
<artifactId>versionsextra</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>path</id>
<phase>test</phase>
<configuration>
<path>${basedir}/src/main/resources/configsys/dev/etc/deploy_env</path>
</configuration>
</execution>
</executions>
So one of the Parameter is called path:
#Parameter()
private String path;
ok, I got it.
I have to declare it like this:
#Mojo(name="devversion")
public class ParameterMojo extends AbstractMojo {
#Parameter()
private String path;
#Parameter()
private String pathsave;
...
And in POM:
<plugin>
<groupId>com.tup.test</groupId>
<artifactId>versionsextra</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>testen</id>
<phase>initialize</phase>
<goals>
<goal>devversion</goal>
</goals>
<configuration>
<path>${basedir}/src/main/resources/configsys/dev/etc/deploy_env</path>
<pathsave>${basedir}/src/main/resources/configsys/dev/etc/test.txt</pathsave>
</configuration>
</execution>
</executions>
</plugin>

Resources