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

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.

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

How to implement a Spring controller which returns any of multiple response types? (multiple 200 code responses)

I have OpenAPI 3 documentation which defines a method:
/header/somemethod:
get:
tags:
- Layout
summary: some text
responses:
200:
description: success
content:
application/json:
schema:
type: array
items:
anyOf:
- $ref: '#/components/schemas/Item1'
- $ref: '#/components/schemas/Item2'
How I can implement a Spring Boot controller which mathes this documentation?
How I can set up swagger annotations to generate this documentation?
I tried to generate a server in Swagger editor, but generated code doesn't show Item1 and Item2.
Thanks.
I believe you have configured pom.xml file for swagger code generation. To get Item1 and Item2, you need to define them in the above file like below:
paths: # path definitions here
definitions:
Item1:
properties:
Item1_name:
description: name
type: string
Item1_value:
description: value
type: number
Item2:
properties:
Item2_name:
description: name
type: string
Item2_value:
description: value
type: number
Note: Here fields in Item1 and Item2 are having fields name and value

Accept only defined parameters for generated Spring-Server

First, I generated a spring-server using https://editor.swagger.io/.
The spring-server works quite well, but ignores these parameters that were not defined by me. So if I send a request to the server with the following JSON,
{
"art": "PK",
"termin": "2019-12-31",
"betrag": 120000,
"test": "test"
}
then I want to get an error, because the parameter "test" is not defined in my swagger.
My swagger code looks like this:
openapi: 3.0.1
info:
...
...
paths:
/vorgang:
post:
tags:
- vorgang
summary: Adds a vorgang
description: Adds a vorgang
operationId: addVorgang
requestBody:
description: procedure object
content:
application/json:
schema:
$ref: '#/components/schemas/Vorgang'
required: true
...
...
components:
schemas:
Vorgang:
type: object
properties:
art:
type: string
enum:
- PK
- FK
termin:
type: string
format: date
betrag:
type: number
format: double
required:
- art
- termin
- betrag

Gets duplicated mapping key error using openapi 3.0

I am trying to define my APIs using openapi version 3.0.0. I've generated following YAML file:
openapi: 3.0.0
info:
title: My YAML
description: My YAML
version: 1.0.0
servers:
- url: http://my.server.url
paths:
/v1/service/uri:
post:
summary: Getting some information.
parameters:
- name: Content-Type
in: header
description: Content type of request body.
required: true
style: simple
explode: false
schema:
type: string
- name: Host
in: header
description: Originate host which returns the response.
required: false
style: simple
explode: false
schema:
type: string
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MyPostRequest'
example:
my_name: "zizi"
my_age: 29
required: true
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/MyPostResponse'
components:
schemas:
MyPostRequest:
type: object
properties:
my_name:
type: string
my_age:
type: integer
format: int32
MyPostResponse:
type: object
properties:
status:
type: integer
format: int32
When I copy/paste these lines into Swagger Editor, it gives me duplicated mapping key error on line 19; it is for section description of parameter Content-Type.
I have studied openapi documentation, but I didn't see anything wrong with my YAML file.
I am not sure why you get that error, I tried to find out in which language Swagger is written, as there are several YAML parsers out there that are known to have problems, but couldn't easily determine that using google or wikipedia.
You don't have any duplicate keys in your file, but it is invalid (i.e. not valid YAML) because of the indentation of the key content (the second occurrence, the one under paths →
/v1/service/uri → post → responses → 200), that should be aligned with
description as that key cannot have a value node that is both a scalar (OK) as well as a mapping node content: ....

Swagger Editor 3.0 How to code component schemas for java maps

I have an response schema that contains java URI, Set and Map.
I can't find any example how to code this yaml part for swagger.
How to write the URI, Set and Map part in yaml components: schemas: see below.
My response java code looks like below.
public class ShowStaticInfo {
private String Id;
private long time;
private URI poster;
private Set<Tag> tags;
private HashMap<String,Tag> objectCreated;
And the written yaml code like this.
As stated above this yaml needs to be changed.
openapi: "3.0.1"
info:
title: Mobile backend
version: 1.0.0
license:
name: Apache 2.0
servers:
- url: https://development.cybercom.com/services/6
description: Development server
- url: https://staging.cybercom.com/services
description: Staging server
- url: https://production.cybercom.com/services
description: Production server
paths:
/buildinfo:
get:
description: Returns the build information (Version and Time stamp).
operationId: getBuildInfo
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/BuildInfo'
parameters:
- in: header
name: LBPATH
schema:
type: string
/countries/{countryId}/cities/{cityId}/showinfo/static:
get:
description: Returns a list of static show information for a city.
operationId: getShowStaticInfo
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ShowStaticInfo'
parameters:
- in: path
name: countryId
required: true
schema:
type: string
- in: path
name: cityId
required: true
schema:
type: string
- in: header
name: Accept-Language
schema:
type: string
- in: header
name: LBPATH
schema:
type: string
components:
schemas:
BuildInfo:
properties:
version:
type: string
timestamp:
type: string
status:
type: string
ShowStaticInfo:
properties:
poster:
$ref: '#/components/schemas/URI'
time:
type: integer
format: int64
showId:
type: string
tags:
$ref: '#/components/schemas/Set'
showObjectCreated:
$ref: '#/components/schemas/HashMap'

Resources