Search in sources :

Example 1 with OpenApi3Parser

use of com.reprezen.kaizen.oasparser.OpenApi3Parser in project policies-ui-backend by RedHatInsights.

the class OapiTest method validateOpenApi.

@Test
void validateOpenApi() throws Exception {
    OpenApi3 model = new OpenApi3Parser().parse(url, true);
    System.out.printf("OpenAPI Model at %s\n", url);
    if (!model.isValid()) {
        for (ValidationResults.ValidationItem item : model.getValidationItems()) {
            System.err.println(item);
        }
        fail("OpenAPI spec is not valid");
    }
    // 
    // Now that basic validation is done, we can add some of our own
    // 
    Map<String, Path> paths = model.getPaths();
    Map<String, Schema> schemas = model.getSchemas();
    // The base path filler. See also OASModifier.mangleName
    assertTrue(paths.containsKey("/"));
    // User config is private, so don't show it
    assertFalse(paths.containsKey("/user-config"));
    assertFalse(schemas.containsKey("SettingsValues"));
    // Check that openapi does not (again) collapse parameters
    assertEquals(9, paths.get("/policies").getOperation("get").getParameters().size());
    // Check that all properties are present ( https://github.com/smallrye/smallrye-open-api/issues/437 )
    Map<String, Schema> policyProperties = schemas.get("Policy").getProperties();
    assertEquals(9, policyProperties.size());
    assertTrue(policyProperties.containsKey("ctime"));
    assertTrue(policyProperties.containsKey("mtime"));
    // Now that the OpenAPI file has been validated, save a copy to the filesystem
    // This file is going to be uploaded in a regular CI build to know the API state
    // for a given build.
    InputStream in = url.openStream();
    Files.copy(in, Paths.get(TARGET_OPENAPI), StandardCopyOption.REPLACE_EXISTING);
}
Also used : Path(com.reprezen.kaizen.oasparser.model3.Path) ValidationResults(com.reprezen.kaizen.oasparser.val.ValidationResults) InputStream(java.io.InputStream) Schema(com.reprezen.kaizen.oasparser.model3.Schema) OpenApi3Parser(com.reprezen.kaizen.oasparser.OpenApi3Parser) OpenApi3(com.reprezen.kaizen.oasparser.model3.OpenApi3) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 2 with OpenApi3Parser

use of com.reprezen.kaizen.oasparser.OpenApi3Parser in project KaiZen-OpenAPI-Editor by RepreZen.

the class OpenApi3Validator method validate.

@Override
public Set<SwaggerError> validate(JsonDocument document, URI baseURI) {
    final Set<SwaggerError> errors = super.validate(document, baseURI);
    final long nbOfErrors = errors.stream().filter(e -> e.getLevel() == SEVERITY_ERROR).count();
    // and option in UI is enable.
    if (isAdvancedValidation() && nbOfErrors == 0) {
        try {
            OpenApi3 result = new OpenApi3Parser().parse(document.get(), baseURI.toURL(), true);
            for (ValidationItem item : result.getValidationResults().getItems()) {
                PositionInfo pos = item.getPositionInfo();
                int line = pos != null ? pos.getLine() : 1;
                errors.add(new SwaggerError(line, getSeverity(item.getSeverity()), item.getMsg()));
            }
        } catch (Exception e) {
            Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, e.getLocalizedMessage()));
        }
    }
    return errors;
}
Also used : Arrays(java.util.Arrays) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument) OpenApi3(com.reprezen.kaizen.oasparser.model3.OpenApi3) Model(com.reprezen.swagedit.core.model.Model) OpenApi3Schema(com.reprezen.swagedit.openapi3.schema.OpenApi3Schema) ValidationItem(com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem) ArrayList(java.util.ArrayList) ArrayNode(com.reprezen.swagedit.core.model.ArrayNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer) PositionInfo(com.reprezen.jsonoverlay.PositionInfo) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) IMarker(org.eclipse.core.resources.IMarker) OpenApi3Parser(com.reprezen.kaizen.oasparser.OpenApi3Parser) Iterator(java.util.Iterator) JsonReferenceValidator(com.reprezen.swagedit.core.json.references.JsonReferenceValidator) Severity(com.reprezen.kaizen.oasparser.val.ValidationResults.Severity) Activator(com.reprezen.swagedit.openapi3.Activator) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) Objects(java.util.Objects) List(java.util.List) Messages(com.reprezen.swagedit.core.validation.Messages) Validator(com.reprezen.swagedit.core.validation.Validator) ADVANCED_VALIDATION(com.reprezen.swagedit.openapi3.preferences.OpenApi3PreferenceConstants.ADVANCED_VALIDATION) SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) JsonSchemaValidator(com.reprezen.swagedit.core.validation.JsonSchemaValidator) SEVERITY_ERROR(org.eclipse.core.resources.IMarker.SEVERITY_ERROR) Status(org.eclipse.core.runtime.Status) SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) PositionInfo(com.reprezen.jsonoverlay.PositionInfo) ValidationItem(com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem) OpenApi3Parser(com.reprezen.kaizen.oasparser.OpenApi3Parser) OpenApi3(com.reprezen.kaizen.oasparser.model3.OpenApi3)

Aggregations

OpenApi3Parser (com.reprezen.kaizen.oasparser.OpenApi3Parser)2 OpenApi3 (com.reprezen.kaizen.oasparser.model3.OpenApi3)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 PositionInfo (com.reprezen.jsonoverlay.PositionInfo)1 Path (com.reprezen.kaizen.oasparser.model3.Path)1 Schema (com.reprezen.kaizen.oasparser.model3.Schema)1 ValidationResults (com.reprezen.kaizen.oasparser.val.ValidationResults)1 Severity (com.reprezen.kaizen.oasparser.val.ValidationResults.Severity)1 ValidationItem (com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem)1 JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)1 JsonReferenceValidator (com.reprezen.swagedit.core.json.references.JsonReferenceValidator)1 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)1 ArrayNode (com.reprezen.swagedit.core.model.ArrayNode)1 Model (com.reprezen.swagedit.core.model.Model)1 JsonSchemaValidator (com.reprezen.swagedit.core.validation.JsonSchemaValidator)1 Messages (com.reprezen.swagedit.core.validation.Messages)1 SwaggerError (com.reprezen.swagedit.core.validation.SwaggerError)1 Validator (com.reprezen.swagedit.core.validation.Validator)1 Activator (com.reprezen.swagedit.openapi3.Activator)1