Swagger-codegen-maven-plugin with feign library build invalid paths - spring

I have API.yaml file from supplier.
When codegen generates an API all my links are invalid because they have a question mark at the end
When i used openapi-generator-maven-plugin for this same API.yaml file, all links were correct.
I can't change swagger to openapi and feign library (project rules).
In project i'm using Feign 11.8 version and feign client has problem with question mark at the end (of course for requests without reqquest params)
How to fix it?
My plugin:
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.35</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/BookAPI.yaml</inputSpec>
<language>java</language>
<configOptions>
<dateLibrary>java8-localdatetime</dateLibrary>
<java8>true</java8>
</configOptions>
<library>feign</library>
<modelPackage>${project.groupId}.client.model</modelPackage>
<apiPackage>${project.groupId}.client.api</apiPackage>
<invokerPackage>${project.groupId}.client.invoker</invokerPackage>
<generateModelTests>false</generateModelTests>
<generateApiTests>false</generateApiTests>
<typeMappings>
<typeMapping>Double=java.math.BigDecimal</typeMapping>
</typeMappings>
</configuration>
</execution>
</executions>
</plugin>
Generated api example:
#RequestLine("POST /api/something/FindSomething?")
#Headers({
"Content-Type: application/json",
"Accept: application/json",
"Authorization: {authorization}"
})
SomethingsResponse findSomething(#Param("authorization") String authorization, SomethingRequest body);
Part of API.yaml
/api/Flight/Something:
post:
tags:
- Something
summary: Something
description: 'Description'
operationId: findSomething
parameters:
- name: Authorization
in: header
description: OAuth 2.0 access token obtained from OAuth2 Token endpoint. Supported grant types: Resource owner password.
required: true
schema:
type: string
requestBody:
description: The request
content:
application/json:
schema:
$ref: '#/components/schemas/SomethingRequest'
example:
Field1: XYZ
Field2: XYZ
Field3:
Field3_1: XYZ
Field3_2: [ ]
responses:
'200':
description: Description.
content:
application/json:
schema:
$ref: '#/components/schemas/SomethingResponse'
'400':
description: Bad Request
content:
application/json: { }
'401':
description: Unauthorized Request
content:
application/json: { }
'403':
description: Forbidden - HTTPS Required.
content:
application/json: { }
'500':
description: Internal Server Error
content:
application/json: { }

Related

How To ignore Endpoints from openapi.yaml in generation with openapi-generator-maven-plugin/

yaml and openapi-generator-maven-plugin is corretly generating Spring Boot Controller Interfaces from it. All is working fine. But now we want to overwrite one of the generated Interfaces with our own interface. How can we exclude a certain endpoint from this generation?
Example:
paths:
/currencies:
get:
tags:
- DomainData
summary: Lists all valid currencies available.
operationId: "getCurrencies"
parameters:
- $ref: '#/components/parameters/AcceptLanguage'
responses:
"200":
description: "OK"
content:
application/json:
schema:
type: string
additionalProperties:
type: string
'401':
$ref: '#/components/responses/Unauthorized'
'5XX':
$ref: '#/components/responses/Unexpected'
/languages:
get:
tags:
- DomainData
summary: Lists all valid languages available.
operationId: "getLanguages"
parameters:
- $ref: '#/components/parameters/AcceptLanguage'
responses:
"200":
description: "OK"
content:
application/json:
schema:
type: string
additionalProperties:
type: string
'401':
$ref: '#/components/responses/Unauthorized'
'5XX':
$ref: '#/components/responses/Unexpected'
This is a part of the openapi.yaml and we would not like to generate for /languages but everything else.
I found a solution we used a .openapi-generator-ignore
and linked it in the pom options of the openapi generator.
In the .openapi-generator-ignore we used the full relative path from position of the ignore file to the file that is not existing and we dont want to get generated.
target/generated-sources/v1/src/main/java/ch/company/dsp/bff/project/generated/api/languageApi.java

Configuring Spring Boot REST API project with yaml file

It is my first time working with Spring Boot REST API. I would like to build a Spring Boot Project with 2 microservices: UserManagement and Messaging.
I have the following yaml file for my UserManagement API endpoint:
swagger: '2.0'
info:
description: UMS Service
version: 1.0.0
title: UMS API
host: ums
basePath: /api/v1
tags:
- name: role
description: Operations about roles
- name: user
description: Operations about users
- name: login/logout
description: Operations about sessions
schemes:
- https
- http
paths:
/role:
post:
tags:
- role
summary: Create role
description: This can only be done by the logged in user.
operationId: createRole
produces:
- application/json
parameters:
- in: body
name: body
description: Created role object
required: true
schema:
$ref: '#/definitions/Role'
responses:
default:
description: successful operation
get:
tags:
- role
summary: Get role by name
description: ''
operationId: getRoleByName
produces:
- application/json
parameters:
- name: rolename
in: query
description: The name that needs to be fetched.
required: true
type: string
responses:
'200':
description: successful operation
schema:
$ref: '#/definitions/Role'
'400':
description: Invalid rolename supplied
'401':
description: Not authenticated
'404':
description: Role not found
put:
tags:
- role
summary: Updated role name
operationId: updateRole
produces:
- application/json
parameters:
- name: rolename
in: query
description: name that need to be updated
required: true
type: string
- in: body
name: body
description: Updated role object
required: true
schema:
$ref: '#/definitions/Role'
responses:
'400':
description: Invalid role supplied
'401':
description: Not authenticated
'404':
description: Role not found
delete:
tags:
- role
summary: Delete role
description: This can only be done by the logged in user.
operationId: deleteRole
produces:
- application/json
parameters:
- name: rolename
in: query
description: The role that needs to be deleted
required: true
type: string
responses:
'400':
description: Invalid role supplied
'401':
description: Not authenticated
'404':
description: Role not found
/user:
post:
tags:
- user
summary: Create user
description: This can only be done by the logged in user.
operationId: createUser
produces:
- application/json
parameters:
- in: body
name: body
description: Created user object
required: true
schema:
$ref: '#/definitions/User'
responses:
default:
description: successful operation
get:
tags:
- user
summary: Get user by user name
description: ''
operationId: getUserByName
produces:
- application/json
parameters:
- name: username
in: query
description: The name that needs to be fetched
required: true
type: string
responses:
'200':
description: successful operation
schema:
$ref: '#/definitions/User'
'400':
description: Invalid username supplied
'401':
description: Not authenticated
'404':
description: User not found
put:
tags:
- user
summary: Updated user
description: This can only be done by the logged in user.
operationId: updateUser
produces:
- application/xml
- application/json
parameters:
- in: body
name: body
description: Updated user object
required: true
schema:
$ref: '#/definitions/User'
responses:
'400':
description: Invalid user supplied
'401':
description: Not authenticated
'404':
description: User not found
delete:
tags:
- user
summary: Delete user
description: This can only be done by the logged in user.
operationId: deleteUser
produces:
- application/json
parameters:
- name: username
in: query
description: The name that needs to be deleted
required: true
type: string
responses:
'400':
description: Invalid username supplied
'401':
description: Not authenticated
'404':
description: User not found
/user/role:
post:
tags:
- user
summary: Assign role to user
description: This can only be done by the logged in user.
operationId: createUserRole
produces:
- application/json
parameters:
- in: body
name: body
description: Created user object
required: true
schema:
$ref: '#/definitions/UserRole'
responses:
default:
description: successful operation
get:
tags:
- user
summary: Get role by user name
description: ''
operationId: getRoleByUserName
produces:
- application/json
parameters:
- name: username
in: query
description: The name that needs to be fetched
required: true
type: string
responses:
'200':
description: successful operation
schema:
$ref: '#/definitions/UserRole'
'400':
description: Invalid username supplied
'401':
description: Not authenticated
'404':
description: User not found
put:
tags:
- user
summary: Updated user's role
description: This can only be done by the logged in user.
operationId: updateUserRole
produces:
- application/json
parameters:
- in: body
name: body
description: Updated user object with new role
required: true
schema:
$ref: '#/definitions/UserRole'
responses:
'400':
description: Invalid username supplied
'401':
description: Not authenticated
'404':
description: User not found
/session/login:
get:
tags:
- login/logout
summary: Get last login for user
description: This can only be done by the logged in user.
operationId: getLastSessionStart
produces:
- application/json
parameters:
- name: username
in: query
required: true
type: string
responses:
'200':
description: successful operation
schema:
$ref: '#/definitions/Session'
'401':
description: Not authenticated
/session/logout:
get:
tags:
- login/logout
summary: Get last logout for user
description: This can only be done by the logged in user.
operationId: getLastSessionEnd
produces:
- application/json
parameters:
- name: username
in: query
required: true
type: string
responses:
'200':
description: successful operation
schema:
$ref: '#/definitions/Session'
'401':
description: Not authenticated
post:
tags:
- login/logout
summary: Set logout for user
description: This can only be done by the logged in user.
operationId: setLastSessionEnd
produces:
- application/json
parameters:
- in: body
name: body
description: Set Log out data for user
required: true
schema:
$ref: '#/definitions/Session'
responses:
default:
description: successful operation
securityDefinitions:
ums_auth:
type: oauth2
authorizationUrl: 'http://ums/oauth/user'
flow: implicit
scopes:
'write:users': modify users in your account
'read:users': read user data
api_key:
type: apiKey
name: api_key
in: header
definitions:
UserRole:
type: object
properties:
username:
type: string
rolename:
type: string
Role:
type: object
properties:
rolename:
type: string
User:
type: object
properties:
username:
type: string
email:
type: string
password:
type: string
role:
type: string
Session:
type: object
properties:
username:
type: string
login:
type: number
logout:
type: number
My question is: how do I configure my UserManagement Service to communicate with this yaml file and generate its contents in a web browser? I am using Eclipse with STS3 plugin.
Thank you for any suggestions. Also, I'd be grateful to sample links to Spring Boot REST API projects with application.yml files which could help me build a simple Spring Boot REST API with 2 services.
You may need a openapi-generator-maven-plugin to read your YML file and generate an OAS (Open API specification) out of it.
Not completely tried from scratch, but check if below helps
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/oas/openapi.yaml</inputSpec>
<generatorName>spring</generatorName>
<modelPackage>com.xyz....</modelPackage>
<apiPackage>com.xyz...</apiPackage>
<output>./../api</output>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<sourceFolder>src/main/java</sourceFolder>
<interfaceOnly>true</interfaceOnly>
<skipDefaultInterface>true</skipDefaultInterface>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
</plugin>
You may reach out to the one who asked a similar question on this article, he might have tried from scratch Openapi operationid is repeated

Uploading multiple file with swagger api documentation and Spring Boot

I want to upload a list of files using swagger api and I can not find a way in which I get the endpoint generated having a list of MultipartFile. Could you please help me? I also searched in the swagger documentation without any luck. For now I have the following swagger config:
x-swagger-router-controller: attachments
post:
operationId: saveFiles
summary: saves the uploaded files
tags:
- files
consumes:
- multipart/form-data
produces:
- application/json
parameters:
- name: files
in: formData
type: array
items:
type: file
description: Files to be uploaded
required: true
- name: type
type: array
items:
type: string
description: File types
in: path
required: true
responses:
201:
description: Saved files
schema:
$ref: '#/definitions/CreatedFilesResponse'
415:
description: Unsupported media type
schema:
$ref: '#/definitions/ErrorResponse'
default:
description: Error
schema:
$ref: '#/definitions/ErrorResponse'
this code generates the following spring endpoint
public ResponseEntity<CreatedAttachmentsResponse> createFile(List<File> list, List<String> list1) {
return null;
}

Open API -> How to reference the external property names/types of Java objects of class path/jar in Request or Response Body YAML Specification

my requirement is to use the properties names and types of the classes which are included in class path java objects.
Basically, $ref will allow us to use the classes which are included in class path like below
1) **YML File**
paths:
/users/{id}:
parameters:
- name: id
in: path
required: true
description: the user identifier, as userId
schema:
type: string
get:
responses:
'200':
description: the user being returned
content:
application/json:
schema:
$ref: "**TESTCALSSOFCLASSPATHORJAR**"
properties:
uuid: # the unique user id
type: string
format: uuid
2) <plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<configuration>
... excluded for simplicity
<importMappings>
<importMapping>TESTCALSSOFCLASSPATHORJAR=path.to.your.TESTCALSSOFCLASSPATHORJAR</importMapping>
</importMappings>
</configuration>
</plugin>
Is there any way that, I can refer id attribute name from my class path/jar java object??
paths:
/users/{id}:
parameters:
- name: **id** -> I want to get this prop from TESTCALSSOFCLASSPATHORJAR class
i.e.TESTCALSSOFCLASSPATHORJAR.id
in: path
required: true
description: the user identifier, as userId
schema:
type: string
get:
responses:
'200':
description: the user being returned
content:
application/json:
schema:
$ref: "**TESTCALSSOFCLASSPATHORJAR**"
properties:
uuid: # the unique user id
type: string
format: uuid
Referred below documents and searching a lot but didn't find any solution.
https://github.com/OAI/OpenAPI-Specification
https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin
Any suggestions would be really appreciated.

How do I access a private swagger API with a maven-build-plugin?

I have generated an API in swagger (API 3.0.0):
Offer-Api:
openapi: 3.0.0
[…]
paths:
/offers:
get:
summary: get all offers
operationId: allOffers
description: return ALL offers
responses:
'200':
description: search results matching criteria
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ApiOffer'
links:
GetOffererById:
$ref: 'https://api.swaggerhub.com/apis/comp/offers/1.0.0#/components/links/GetSingleOffererById'
GetOfferById:
$ref: '#/components/links/GetSingleOfferById'
'400':
description: something went terribly wrong
[…]
I copied that content into my project as an „Offers.yaml“ and try to compile it via „openapi-generator-maven-plugin“
[…]
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
[…]
<execution>
<id>Offers</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/Offers.yaml</inputSpec>
<modelPackage>${project.modelpackage}</modelPackage>
<apiPackage>${project.apipackage}</apiPackage>
<generateApis>false</generateApis>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModels>true</generateModels>
<generateModelTests>true</generateModelTests>
<withXml>true</withXml>
<generateModelDocumentation>false</generateModelDocumentation>
<generateSupportingFiles>false</generateSupportingFiles>
<generatorName>java</generatorName>
<library>jersey2</library>
<validateSpec>false</validateSpec>
<configOptions>
<sourceFolder>src/gen/java/</sourceFolder>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
[…]
During maven-compile following error occurs:
[INFO]
[INFO] --- openapi-generator-maven-plugin:3.3.0:generate (Offers) # server ---
[ERROR] unable to read
java.io.FileNotFoundException: https://api.swaggerhub.com/apis/comp/offerers/1.0.0
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0 (HttpURLConnection.java:1890)
Sure, https://api.swaggerhub.com/apis/comp/offerers/1.0.0 is a PRIVATE API. But is there a way to tell the maven plugin the correct credentials, so that it can access that API anyway?

Resources