openapi-processor / openapi-parser

OpenAPI 3.0/3.1 Parser & JSON Schema Validator, Java
Apache License 2.0
12 stars 1 forks source link

openapi validation question #95

Open matteo-rama opened 1 month ago

matteo-rama commented 1 month ago

hi i am trying to validate an openapi 3 file, json format,

using other tools i got {"errors":[{"code":400,"message":"Malformed OAS3 document: attribute components.schemas.Schema name ResponsePayloadByte[] doesn't adhere to regular expression ^[a-zA-Z0-9\.\-_]+$"}

and also this stuff https://quobix.com/vacuum/ return some validation error.

if i try using openapi-parser lib inside a java app, i don't get any validation error, the file is correctly parsed and it work fine.

am i missing anything? shouldn't it fail?

i am attaching file that give error

thanks

PFM_User_Dots.json

hauner commented 1 month ago

Yes, I think it should report that error. I will look at it.

hauner commented 1 month ago

For me validating your file I get

should conform to rfc3968 at instance /paths/~1pdf~1documents~1{imageId}/get/responses/200/content/*~1*/schema/$ref (schema #/definitions/Reference/patternProperties/^\$ref$/format)

It complains about this invalid ref: "$ref": "#/components/schemas/ResponsePayloadByte[]".

This corresponds to the error you expect. I guess the other tools inlines ResponsePayloadByte[] and then complains about its name.

it reports a few more errors but they are all related to the error above. I should probably filter out the other errors.

Here is the code I'm using (it is kotlin test code but hopefully easy enough to understand)

import io.kotest.core.spec.style.StringSpec
import io.openapiprocessor.jackson.JacksonConverter
import io.openapiprocessor.jsonschema.reader.UriReader
import io.openapiprocessor.jsonschema.schema.DocumentLoader
import io.openapiprocessor.jsonschema.schema.DocumentStore
import io.openapiprocessor.jsonschema.schema.SchemaStore
import io.openapiprocessor.jsonschema.validator.Validator
import io.openapiprocessor.jsonschema.validator.ValidatorSettings

class ValidateSpec: StringSpec({

    "validate openapi" {
        val documents = DocumentStore()
        val loader = DocumentLoader(
            UriReader(),
            JacksonConverter()
        )

        val parser = OpenApiParser(documents, loader)
        val result = parser.parse("/v30/openapi-95.json")

        val store = SchemaStore(loader)
        store.registerDraft4()

        val validator = Validator()
        val valid = result.validate(validator, store)

        val errors = result.validationErrors
        val builder = ValidationErrorTextBuilder()

        for (error in errors) {
            println(builder.getText(error))
        }
    }
)}
matteo-rama commented 1 month ago

what happen if you don't use "registerDraft4"? if i don't use "registerdraft4" i see no error, still it shouldn't validate if i understand correctly?


        OpenApiParser parser = new OpenApiParser(new DocumentStore(), loader);

        OpenApiResult result = parser.parse(swaggerFile.toUri());

        SchemaStore store = new SchemaStore(loader);
        store.registerDraft4();

        ValidatorSettings settings = new ValidatorSettings();
        Validator validator = new Validator(settings);
        boolean valid = result.validate(validator, store);
hauner commented 1 month ago

Yes. The register just avoids a network call.

It adds the draft-4 json schema from the jar resources. Without the register it will download the json schema.

matteo-rama commented 1 month ago

i am sorry to bother you again...

but if the enable draf4 just avoid network calls, then, why this snippet validate?


        DocumentLoader loader = new DocumentLoader(new io.openapiprocessor.jsonschema.reader.UriReader(), s -> {
            try {
                if (swaggerFile.endsWith(".json"))
                    return json.readValue(s, Object.class);
                else
                    return yml.readValue(s, Object.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });

        OpenApiParser parser = new OpenApiParser(new DocumentStore(), loader);

        OpenApiResult result = parser.parse(swaggerFile.toUri());

        SchemaStore store = new SchemaStore(loader);
       // store.registerDraft4();

        ValidatorSettings settings = new ValidatorSettings();
        Validator validator = new Validator(settings);
        boolean valid = result.validate(validator, store);

        if (!valid)
            throw new JobFailure("invalid openapi file for", KO_VAR, swaggerFile);

strangely if i remove the comment // store.registerDraft4 i got the exception

hauner commented 1 month ago

Yes, strange indeed. Using your code works for me. With or without the register using your example api.

One difference is that I'm using JacksonConverter() as I don't know what json and yml is in your snippet. But I would expect an exception if something goes wrong there.

If you can create a minimal working project that reproduces the issue I could check what is going.