cancel
Showing results for 
Search instead for 
Did you mean: 

How can I use swagger api functionality to generate response example

Highlighted
Occasional Contributor

How can I use swagger api functionality to generate response example

I am working on a project where I have to generate json representation of method return type.
I have seen swagger api is generating response example and wanted to know, how can I use swagger api functionality to generate response example and use it internally.
Any help would be highly appreciated.
Thanks
6 REPLIES 6
Highlighted
Moderator

Re: How can I use swagger api functionality to generate response example

Hi @rajrahul9939,

 

Just to clarify - do you want to generate an example of JSON payload based on a schema definition? Or do you need a mock server that would return sample responses? Or something else?


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Occasional Contributor

Re: How can I use swagger api functionality to generate response example

Hi  @HKosova 

 

Highlighted
Moderator

Re: How can I use swagger api functionality to generate response example

You can use the Swagger Inflector Java library, specifically its ExampleBuilder class, to generate JSON examples. Here's sample code:

https://stackoverflow.com/questions/41408768/how-to-generate-json-examples-from-openapi-swagger-mode...


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Occasional Contributor

Re: How can I use swagger api functionality to generate response example

Hey @HKosova 

swagger-inflector, only works when the schema is defined, Here is more details about my query.

I have an API "/hello" defined below.

api.PNG

and Person class like below.

Person.PNG

I wanted to generate a response example like below.

response.PNG

Thank you.

Highlighted
Occasional Contributor

Re: How can I use swagger api functionality to generate response example

Hey @HKosova ,

could you please help 

Highlighted
Moderator

Re: How can I use swagger api functionality to generate response example


@rajrahul9939 wrote:

swagger-inflector, only works when the schema is defined


ExampleBuilder does not necessarily need a named schema; it can use any schema, including those defined inline - you just need to pass in the appropriate parameters. And the parser has methods to get the response schema for a specific path, HTTP method, and response code

 

In your case, to generate an example for the path /hello, method GET, and HTTP status 200 you can use:

Model responseSchema = swagger.getPath("/hello")
.getOperationMap().get(HttpMethod.GET)
.getResponses().get("200")
.getResponseSchema(); Example example = ExampleBuilder.fromModel(null, responseSchema, definitions, new HashSet<String>());

 The rest of the code is the same as here.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Join us and watch the welcome video:
Top Kudoed Authors