Issue
I am using openapi-generator-maven-plugin
to generate the server code for a springboot application.
However I can't figure out a easy way to generate the @RequestHeader("header")
in the interface.
Here is the plugin configuration:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>5.1.0</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/spec/api.yaml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>my.api</apiPackage>
<modelPackage>my.models</modelPackage>
<configOptions>
<delegatePattern>false</delegatePattern>
<interfaceOnly>true</interfaceOnly>
<useTags>true</useTags>
<useOptional>true</useOptional>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
API spec:
get:
tags:
- template
summary: find template by template name
description: returns single template
operationId: getTemplateByName
parameters:
- name: bucket
in: path
description: location
required: true
schema:
type: string
- name: templateName
in: path
description: the name of the template
required: true
schema:
type: string
responses:
200:
description: successful operation
content:
multipart/mixed:
schema:
type: object
application/json:
schema:
$ref: '#/components/schemas/Template'
400:
description: Invalid input
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
404:
description: template not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
The generated interface:
@ApiOperation(value = "find template by template name", nickname = "getTemplateByName", notes = "returns single template", response = Object.class, tags={ "template", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response = Object.class),
@ApiResponse(code = 400, message = "Invalid input", response = Error.class),
@ApiResponse(code = 404, message = "template not found", response = Error.class) })
@ApiImplicitParams({
})
@GetMapping(
value = "/bu/{bucket}/templates/{templateName}",
produces = { "multipart/mixed", "application/json" }
)
default ResponseEntity<Object> getTemplateByName(@ApiParam(value = "location",required=true) @PathVariable("bucket") String bucket,@ApiParam(value = "the name of the template",required=true) @PathVariable("templateName") String templateName) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
How to add @RequestHeader to the interface? or is there a way it can generate the httprequest in the interface?
Solution
As mentioned at swagger Docs this can be done using header
like the following
paths:
/ping:
get:
summary: Checks if the server is alive
parameters:
- in: header
name: X-Request-ID
schema:
type: string
format: uuid
required: true
In a similar way, you can define custom response headers. Header parameter can be primitives, arrays and objects. Arrays and objects are serialized using the simple style.
Note: Header parameters named Accept, Content-Type and Authorization are not allowed. To describe these headers, use the corresponding OpenAPI keywords:
Answered By - George
Answer Checked By - David Goodson (JavaFixing Volunteer)