I’m trying to generate client from yaml that contains
JavaScript
x
acceptParam:
name: Accept
type: string
required: true
in: header
description: Accepted Content-type. Should be set to application/json
contentTypeParam:
name: Content-Type
type: string
required: true
in: header
description: Request Content-type. Should be set to application/json
That means, accept
and contentType
will be present in generated method signature.
On top of that, I’v configured plugin like this
JavaScript
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.18</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger.yaml</inputSpec>
<language>java</language>
<configOptions>
<dateLibrary>joda</dateLibrary>
<localVarPrefix>localVar</localVarPrefix>
</configOptions>
<library>resttemplate</library>
<output>${project.build.directory}/generated-sources</output>
<modelPackage>com.example.client.model</modelPackage>
<apiPackage>com.example.client.api</apiPackage>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
</configuration>
</execution>
</executions>
</plugin>
Still, after mvn clean install
I’m getting
Error:(130,31) java: variable accept is already defined in method
And generated code contains
JavaScript
public Response authorize(Request body, String accept, ) {
.
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
.
}
I’v tried different versions of plugin (from 2.3.0 up to newest one), after overcoming lots of other issues, I’m always ending up like this.
Advertisement
Answer
In OpenAPI 2.0, the Accept
and Content-Type
headers should be defined using consumes
and produces
rather than parameters. In OpenAPI 3.0, these headers are defined as request/response media types.
Change your operation definition as follows:
JavaScript
swagger: '2.0'
paths:
/foo:
post:
consumes:
- application/json
produces:
- application/json
or if you use OpenAPI 3.0:
JavaScript
openapi: 3.0.0
paths:
/foo:
post:
requestBody:
content:
application/json: # <----
schema:
responses:
'200':
description: ok
content:
application/json: # <----
schema: