agliznetsov / swagger-tools Goto Github PK
View Code? Open in Web Editor NEWCode generator to produce java client and server mappings from Swagger / OpenAPI REST API definition.
License: MIT License
Code generator to produce java client and server mappings from Swagger / OpenAPI REST API definition.
License: MIT License
The following spec shows the use of a default value for the enumProp
field:
EnumExample:
type: string
enum:
- One
- Two
ObjectExample:
type: object
properties:
enumProp:
$ref: '#/components/schemas/EnumExample'
default: 'One'
When generating the model classes, the 'default' value is not taken into account. It does work for simple datatypes though, but not for enumeration types that are referenced.
Hi Andrey,
How are you?
We still use this plugin to generate our rest api classes. :)
And so when the inherited class is generated the lombok ToString annotation is added without a callSuper.
And so the properties of the super class are not added in the string representation.
f.e.
`
package com.evs.phoenix.transform.payloads;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.lang.Boolean;
import java.lang.String;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@tostring
@EqualsAndHashCode
public class FileLocation extends Location {
@JsonProperty("_type")
private final String type = "FileLocation";
@JsonProperty("present")
private Boolean present;
public String getType() {
return type;
}
public Boolean isPresent() {
return present;
}
public void setPresent(Boolean present) {
this.present = present;
}
}
`
Consider the following spec:
paths:
/animals:
post:
operationId: createAnimals
requestBody:
description: animal
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Animal'
required: true
components:
Animal:
type: object
properties:
name:
type: string
discriminator:
propertyName: '@type'
Dog:
allOf:
- $ref: '#/components/schemas/Animal'
Cat:
allOf:
- $ref: '#/components/schemas/Animal'
When you generate a client
for this spec, it will fail because the generated code is not providing the type information to the client library (e.g. resttemplate, webclient). Jackson needs this information as it cannot know the type from an incoming List of objects.
The invokeApi method (in this example for resttemplate) should be something similar to (note the additional requestBodyTypeRef param):
protected <T, U> ResponseEntity<T> invokeAPI(String path, String method, Map<String, String> urlVariables, MultiValueMap<String, String> queryParams, Object body, ParameterizedTypeReference<U> requestBodyTypeRef, ParameterizedTypeReference<T> returnType) {
URI baseUrl = restTemplate.getUriTemplateHandler().expand(basePath);
URI uri = UriComponentsBuilder
.fromUri(baseUrl)
.path(path)
.queryParams(queryParams)
.buildAndExpand(urlVariables)
.toUri();
RequestEntity.BodyBuilder requestBuilder = RequestEntity.method(HttpMethod.resolve(method), uri);
customizeRequest(requestBuilder);
RequestEntity<Object> requestEntity = requestBuilder.body(body, requestBodyTypeRef.getType());
return restTemplate.exchange(requestEntity, returnType);
}
There is a property in OpenAPI called ‘uniqueItems’ which you can use to indicate that the items inside an array should be unique (see https://swagger.io/docs/specification/data-models/data-types/#array). If this property is set to true, it would be nice if the Java Set type is used in the generated code, instead of a List type.
Consider the following spec, where we declare a GET that returns a list of items:
/list:
get:
tags:
- Example
operationId: getExampleList
responses:
200:
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Example'
When generating a client for this method, using the WebClient dialect, it results in the following code fragment:
public Mono<List<Example>> getExampleList() {
ParameterizedTypeReference<List<Example>> typeRef = new ParameterizedTypeReference<List<Example>>(){};
return invokeAPI("/list", "GET", createUrlVariables(), createQueryParameters(), null).bodyToMono(typeRef);
}
While I would expect the following:
public Flux<Example> getExampleList() {
return invokeAPI("/list", "GET", createUrlVariables(), createQueryParameters(), null).bodyToFlux(Example.class);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.