I have a javax.json.Json object which I need to validate if its a valid Swagger file or not. I wrote these utility functions -
package com.somecompany.gis.util; import java.io.IOException; import java.io.StringWriter; import java.util.Map; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonWriter; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.models.Swagger; import io.swagger.parser.SwaggerParser; public class Converter { public static JsonNode toJsonNode(JsonObject jsonObject) throws IOException { // Parse a JsonObject into a JSON string StringWriter stringWriter = new StringWriter(); try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) { jsonWriter.writeObject(jsonObject); } String json = stringWriter.toString(); // Parse a JSON string into a JsonNode ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(json); return jsonNode; } public static boolean isValidSwaggerSpec(JsonObject jsonObject) { try { JsonNode jsonNode = toJsonNode(jsonObject); Swagger swagger = new SwaggerParser().read(jsonNode); return true; }catch(IOException ioe) { return false; }catch(Exception e) { return false; } } However, I see that even with an invalid Swagger file, i get an evaluation of true. Is there any way I can check if a Swagger is valid or not?
SwaggerParser#readthrows any exceptions, does it returnnullwhen you provide an invalid configuration? What version of Swagger spec do you want to validate?ParseExceptionon reading invalid JSON. But no such exception. However, most getters of the Swagger provide menull.