Use OpenAPIParser to read OpenAPI specifications from a URL, a file path, or a string. The parser returns a SwaggerParseResult which contains the parsed OpenAPI POJO and any validation messages (errors or warnings).
Note: If you provide a Swagger/OpenAPI 2.0 document, it will be automatically converted to OpenAPI 3.0.
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.oas.models.OpenAPI;
// Parse from a URL or file location
SwaggerParseResult result = new OpenAPIParser().readLocation("https://petstore3.swagger.io/api/v3/openapi.json", null, null);
// Parse from string contents
// SwaggerParseResult result = new OpenAPIParser().readContents("contents", null, null);
OpenAPI openAPI = result.getOpenAPI();
// Handle validation errors and warnings
if (result.getMessages() != null) {
result.getMessages().forEach(System.err::println);
}
if (openAPI != null) {
// Use the parsed model
}