swagger-codegen is referencing a non-existent Java class - swagger

I'm using swagger-codegen to create a Java stub around a Hyperledger Composer REST Server, but codegen is referencing a class that it hasn't generated.
Sample input:
"responses": {
"200": {
"description": "Request was successful",
"schema": {
"$ref": "#/definitions/Long"
}
}
},
Checking the definitions section of the swagger.json file, there is no definition for Long.
The Java stub created includes this import, but there is no corresponding ModelLong class in that package:
import io.swagger.client.model.ModelLong;
The API stub code itself looks like this:
public ModelLong allowanceCreatePostAllowance(Allowance data) throws ApiException {
ApiResponse<ModelLong> resp = allowanceCreatePostAllowanceWithHttpInfo(data);
return resp.getData();
}
Is this a class I should expect to be writing myself or could there be some error in the mapping?

Related

Swagger + Nest.js doesn't remove empty DTO and model

I was creating documentation for Nest.js API using Swagger. The problem is what I removed documentation from this DTO or model, in swagger UI docs I can see it as empty object.
For example:
import { IsNotEmpty } from 'class-validator';
export class PostDto {
#IsNotEmpty()
readonly title: string;
#IsNotEmpty()
readonly content: string;
#IsNotEmpty()
readonly description: string;
}
Also I was trying to change name of this entity, using incognito mode, reinstall node_modules, but it didn't work. If I change name of this entity, it also changes there. What's wrong?
What I want to do, is by removing this documentation decorators, not to see those empty objects.
There is a CLI plugin provided with the #nestjs/swagger which will automatically add the annotations using reflection.
This means you NO LONGER need to individually annotate each property using #ApiProperty() etc.
The plugin is opt-in and not active by default.
In your nest-cli.json add:
{
"collection": "#nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": ["#nestjs/swagger"]
}
}
You can also change the default options:
"plugins": [
{
"name": "#nestjs/swagger",
"options": {
"classValidatorShim": false,
"introspectComments": true
}
}
]
See https://docs.nestjs.com/openapi/cli-plugin
Troubleshooting: if you are running in dev mode, may need to stop/start.

How to document a wrapped response to be displayed in swagger ui using a Swashbuckle in asp.net core web api

I working on a ASP.NET Core 3.1 web api project. I'm using Swashbuckle.AspNetCore 5.0.0 for documenting my API. Things are working good. However I got stuck with generating response types as my api is using an middleware to wrap every response for consistency. I'm not able to generate correct response type in my swagger ui.
Here is an simple example,
My Action Method:
[HttpGet]
[ProducesResponseType(200, Type = typeof(IEnumerable<WeatherForecast>))]
public IEnumerable<WeatherForecast> Get()
...
As I mentioned, the project has response middleware which will wrap all the response as shown in the below format,
{
"Version": "1.0.0.0",
"StatusCode": 200,
"Message": "Request successful.",
"Result": [
"value1",
"value2"
]
}
Because of this I'm getting mismatch in response value in my swagger ui.
Example of response schema shown in swagger ui as per [ProducesResponseType(200, Type = typeof(IEnumerable<WeatherForecast>))]
But the actual wrapped response looks like,
Is it possible to handle these wrapped response using Swashbuckle.AspNetCore 5.0.0. Please assist me.
After some analysis and research, I found the solution. It's pretty simple using the [ProducesResponseType] attribute.
I created a separate class named ResponseWrapper<T>,
public class ResponseWrapper<T>
{
public int StatusCode { get; set; }
public string Message { get; set; }
public T Result { get; set; }
}
And then decorated my action method as follows,
[HttpGet]
[ProducesResponseType(200, Type = typeof(ResponseWrapper<IEnumerable<WeatherForecast>>))]
public IEnumerable<WeatherForecast> Get()
...
And that works. Hope this helps someone.

Elasticsearch repository not allowing default field type

While creating an index Elasticsearch Repository does not allow default type. If field type annotation is missing, assuming default type, the Spring data elasticsearch mapper throws exception and then creates some default mapping when I save the first object.
I'm wondering if it is somehow possible to not annotate every field in of my data objects?
I'm using Spring Data Elasticsearch v3.1.8 and Elasticsearch 6.2.2.
Thanks
There is effectively a way to avoid annoting every fields. Elasticsearch need to create the mapping of the document.
You can indicate to spring how the document need to be mapped with #Field or by providing the mapping configuration.
With spring just annotate the document with #Mapping to set mapping file location.
Also you could just create the mapping using Elasticsearch PUT mapping API.
#Document(indexName = Produit.INDEX, type = ProduitES.TYPE, shards = 1)
#Mapping(mappingPath = "/mappings/mappings-produit.json")
public class Produit {
private String code;
private String name;
...
}
The mapping file in resources/mappings/mappings-produit.json folder :
{
"produit": {
"dynamic": "strict",
"properties": {
"code": {
"type": "text",
},
"name": {
"type": "text",
"index": false
}
}
}
}

Swagger is stripping the website name off the Base URL

I'm using swagger but when I host my webapi in IIS, swagger is stripping the website name off. The website is a virtual directory/application in IIS called LotsWebApi.
When I hit this url: http://localhost/LotsWebApi/swagger/ui/index.html
I get "Can't read swagger JSON from http://localhost/swagger/v1/swagger.json"
You can see that it is stripping off the 'LotsWebApi'.
My register swagger code looks like this...
private static void RegisterSwagger(IServiceCollection services)
{
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "LOTS Web API",
Description = "API for LOTS Web"
});
});
services.AddSwaggerGen();
}

How will Swagger 2.0 handle generics in parameters or return types?

I am using Swagger 2.0 for documentation generation. In my controller class, I have some operations like:
public Page<Employee> getEmployees(Pageable pageable) {....}
Swagger document generated for response of the operation above:
"responses" : {
"200" : {
"schema" : {
"$ref" : "#/definitions/Page"
}
}
}
Here, Swagger documentation failed to say that response is Page<Employee>. How do I get generics data in documentation in Swagger?
And what if I have the following return types?
Page<String,Employee>
Page<Employee,List<Department>>
Page<Employee,Tuple.Two<String,User>>
Same is true for Swagger Operation parameters and Model properties.
This is a known issue with Swagger, please check the following issue: https://github.com/OAI/OpenAPI-Specification/issues/957. Point being, you'll need to create your own extensions to describe generics.

Resources