Forum Discussion
Hi HKosova
Thank you for your response.
I wanted to generate an example of JSON payload based on a method return type, same as swagger generate the response example.
Thank you
You can use the Swagger Inflector Java library, specifically its ExampleBuilder class, to generate JSON examples. Here's sample code:
- rajrahul99395 years agoOccasional Contributor
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.
and Person class like below.
I wanted to generate a response example like below.
Thank you.
- rajrahul99395 years agoOccasional Contributor
Hey HKosova ,
could you please help
- HKosova5 years agoSmartBear Alumni (Retired)
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.