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

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.

Related

SpringBoot validation doesn't work with openapi

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);
}

Maven To Gradle - Conversion

I am trying to convert an old maven to gradle. Below is the maven block I am trying to convert to gradle equivalent.
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generateDirectory>${project.basedir}/target/generated-sources/twg</generateDirectory>
<schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
<schemaIncludes>
<include>TWG.wsdl</include>
</schemaIncludes>
</configuration>
</plugin>
</plugins>
I tried something like this below
task generateJavaClasses {
System.setProperty('javax.xml.accessExternalSchema', 'all')
def jaxbTargetDir = file("src/main/java/")
doLast {
jaxbTargetDir.mkdirs()
ant.taskdef(
name: 'xjc',
classname: 'com.sun.tools.xjc.XJCTask',
classpath: configurations.jaxb.asPath
)
ant.jaxbTargetDir = jaxbTargetDir
ant.xjc(
destdir: '${jaxbTargetDir}',
package: 'newjavaFromWsdl',
schema: 'src/main/resources/TWG.wsdl',
language: 'WSDL'
)
}
}
added the jaxb dependency like
dependencies {
implementation 'com.sun.xml.bind:jaxb-xjc:4.0.0'
}
but not working actually, what am i doing wrong here
There was no issue with the script, but in Gradle, the dependency i used was wrong.
it should be like
configurations {
jaxb
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:2.7.1'
jaxb(
'com.sun.xml.bind:jaxb-core:3.0.2',
'com.sun.xml.bind:jaxb-xjc:2.3.6',
'com.sun.xml.bind:jaxb-impl:2.3.6'
)
}

Jaxb2 maven plugin getting error when generating xsd from complex classes

I have a case that i have 35 classes that some of them related with each other inside of them. Such as;
Addendum.java
#XmlType(name="addendum",namespace= GenericNameSpaceConstants.POLICY_NAMESPACE_URI)
#XmlAccessorType(XmlAccessType.FIELD)
public class Addendum implements Serializable {
#XmlElement(name="changeNumber",nillable=false,required=true)
private Long changeNumber;
#XmlElement(name="changeTypeDesc",nillable=false,required=true)
private String changeTypeDesc;
#XmlElement(name="changeTypeId",nillable=false,required=true)
private Integer changeTypeId;
}
Policy.java
#XmlRootElement(name="policy",namespace=GenericNameSpaceConstants.POLICY_NAMESPACE_URI)
#XmlType(name="policy",namespace= GenericNameSpaceConstants.POLICY_NAMESPACE_URI)
#XmlAccessorType(XmlAccessType.FIELD)
public class Policy {
#XmlElement(name="addendum",required=true,nillable=false)
private Addendum addendum;
}
My jaxb schemage config in pom file like that
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<createJavaDocAnnotations>false</createJavaDocAnnotations>
<sources>
<source>
${project.basedir}\src\main\java\com\aegon\common\service\bean\
</source>
</sources>
<verbose>true</verbose>
<outputDirectory>${basedir}/src/main/resources/schemas</outputDirectory>
<transformSchemas>
<transformSchema>
<toPrefix>pol</toPrefix>
<toFile>policy_model_v2.xsd</toFile>
</transformSchema>
</transformSchemas>
<generateEpisode>true</generateEpisode>
</configuration>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
</plugin>
When i run the project for phase generate-resources or generate-sources. I am getting this error Addendum is a non-static inner class, and JAXB can't handle those.
How can i resolve this problem?? How can i generate all classes xsd in a simple xsd Or how can i create xsds' one by one and import to complex one
I have found the problem. every class need a default constructor

Not able to link my javadocs with spring auto rest docs

I am not getting how do you use JavaDocs, with Spring Auto Rest Docs. I can generate my java-docs locally by using STS, UI option. However,i am not sure how to generate java-docs using Spring Auto Rest Docs. I tried writing some plugin in POM. But the auto-description.adoc and many other doc are still empty.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<!-- switch on dependency-driven aggregation -->
<includeDependencySources>true</includeDependencySources>
</configuration>
</execution>
<execution>
<id>generate-javadoc-json</id>
<phase>compile</phase>
<goals>
<goal>javadoc-no-fork</goal>
</goals>
<configuration>
<doclet>capital.scalable.restdocs.jsondoclet.ExtractDocumentationAsJsonDoclet</doclet>
<docletArtifact>
<groupId>capital.scalable</groupId>
<artifactId>spring-auto-restdocs-json-doclet</artifactId>
</docletArtifact>
<additionalparam>
-d ${project.build.directory}/site/doccheck
</additionalparam>
<reportOutputDirectory>${project.build.directory}</reportOutputDirectory>
<useStandardDocletOptions>false</useStandardDocletOptions>
<show>package</show>
</configuration>
</execution>
</executions>
</plugin>
Basically I am not getting how to make Spring Auto Rest Docs use Java Docs to find Path Parameters. I can use mvn javadoc:javadoc command on terminal to create Javadocs whch are created in target/site/apidocs/ folder. But I still get No parameters in my auto-path-parameters.adoc
Here is the controller, I am using GraphQL so its different:
/**
* Query Controller Class
* #author shantanu
*
*/
#RestController
#RequestMapping(value="/graphql")
public class QueryController {
#Value("classpath:/graphql/actionItm.graphqls")
private Resource schemaResource;
private GraphQL graphQL;
#Autowired
#Qualifier("graphQLDate")
private GraphQLScalarType Date;
#Autowired
private AllActionItemsDataFetcher allActionItemsDataFetcher;
#Autowired
private ActionItemDataFetcher actionItemDataFetcher;
#Autowired
private PageActionItemDataFetcher pageActionItemDataFetcher;
#PostConstruct
public void loadSchema() throws IOException {
File schemaFile = schemaResource.getFile();
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
graphQL = GraphQL.newGraphQL(schema).build();
}
private RuntimeWiring buildRuntimeWiring() {
return RuntimeWiring.newRuntimeWiring()
.type("Query", typeWiring -> typeWiring
.dataFetcher("findAllActionItems", allActionItemsDataFetcher)
.dataFetcher("findActionItem", actionItemDataFetcher)
.dataFetcher("pageActionItems", pageActionItemDataFetcher))
.scalar(Date)
.build();
}
/**
* This
* #param query Description
* #return ResponseEntity responseEntity
*/
#PostMapping
public ResponseEntity query(#RequestBody String query) {
ExecutionResult result = graphQL.execute(query);
return ResponseEntity.ok(result.getData());
}
}
I am not getting where I am wrong, with the path-parameters. And only the .adoc files are generated with Spring Auto Rest Docs, and no JSON files.

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