swagger-api / swagger-inflector

Apache License 2.0
160 stars 85 forks source link

Convert OAS on YAML format into JSON format #423

Closed jabrena closed 2 years ago

jabrena commented 2 years ago

Hi,

I was following a question in Stack Overflow about how to convert an OAS in YAML format into JSON format: https://stackoverflow.com/questions/55978052/how-to-generate-json-examples-from-openapi-3-0-yaml-file

and I was reviewing the examples from the repository: https://github.com/swagger-api/swagger-inflector/blob/master/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java https://github.com/swagger-api/swagger-inflector/blob/master/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java

But I don't find a way to do it with the Class ExampleBuilder.

Can you help me?

OAS Example: https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore.yaml

Code:


String path ="/petstore.yaml";

OpenAPI swagger = new OpenAPIV3Parser().read(path);
Map<String, Schema> definitions = swagger.getComponents().getSchemas();
Schema model = definitions.get("pets");
Example example = ExampleBuilder.fromSchema(model, definitions);
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
String jsonExample = Json.pretty(example);

System.out.println(jsonExample);

Currently, the response is null, but I would like to receive the whole spec converted into json format. What is wrong in my example?

Many thanks in advance

Juan Antonio

jabrena commented 2 years ago

Solved the issue in another way