Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure of ExampleBuilder Class to Parse JSON Schema, Resulting in Blank JSON Example when using Swagger Parser and Swagger Inflector #471

Open
RonakPrajapatiPanamax opened this issue May 1, 2024 · 2 comments

Comments

@RonakPrajapatiPanamax
Copy link

Problem

  • I am encountering an issue when trying to parse OpenAPI JSON 3.1.0 examples of the Petstore. It seems that the ExampleBuilder class from io.swagger.oas.inflector.examples.ExampleBuilder does not support parsing JSON Schema, which is generated by the Swagger parser for 3.1.0 OpenAPI specs.

Observation

  • Upon reviewing the code of the ExampleBuilder class, it appears that there is no support for parsing JSON Schema.

Steps to Reproduce

  • Obtain the Petstore OpenAPI JSON 3.1.0 example.
  • Attempt to parse it using the Swagger parser and utilize the ExampleBuilder class to generate a JSON Example from the schema object.

Expected Behavior

  • The ExampleBuilder class should be able to parse the JSON Schema generated by the Swagger parser for 3.1.0 OpenAPI specs.

Actual Behavior

  • The ExampleBuilder class fails to parse the JSON Schema, resulting in a blank JSON example without keys and values.

Code Snippet.

		ParseOptions parseOptions = new ParseOptions();
		parseOptions.setResolve(true);
		parseOptions.setResolveFully(true);
		SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation(specUrl, null, parseOptions);

		if (parseResult.getMessages() != null && !parseResult.getMessages().isEmpty()) {
			for (String message : parseResult.getMessages()) {
				System.out.println("Parsing message: " + message);
			}
		}
		OpenAPI openAPI = parseResult.getOpenAPI();
		Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
		Schema model = definitions.get("Pet");
		Example example = ExampleBuilder.fromSchema(model, model.getProperties());

		SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
		Json.mapper().registerModule(simpleModule);
		String jsonExample = Json.pretty(example);
		System.out.println(jsonExample);

Dependencies (from pom.xml)

<dependencies>
    <dependency>
        <groupId>io.swagger.parser.v3</groupId>
        <artifactId>swagger-parser</artifactId>
        <version>2.1.22</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-inflector</artifactId>
        <version>2.0.12</version>
    </dependency>
</dependencies>

@fehguy and @gracekarina I have taken latest swagger-inflector version. If anyone knows any solution then please let me know.

@fehguy
Copy link
Contributor

fehguy commented May 1, 2024

Please share where you're getting the 3.1 pet store document?

@RonakPrajapatiPanamax
Copy link
Author

RonakPrajapatiPanamax commented May 2, 2024

@fehguy Thanks for reply,
You can find open API 3.1 Spec file.
URL : https://editor-next.swagger.io/
File --> Load Example --> OpenAPI 3.1 Petstore

I am also attaching same file here.
petStore3.1.0.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants