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

openapi validation question #95

Open
matteo-rama opened this issue Jun 6, 2024 · 6 comments
Open

openapi validation question #95

matteo-rama opened this issue Jun 6, 2024 · 6 comments

Comments

@matteo-rama
Copy link

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
Copy link
Member

hauner commented Jun 7, 2024

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

@hauner
Copy link
Member

hauner commented Jun 8, 2024

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
Copy link
Author

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
Copy link
Member

hauner commented Jun 11, 2024

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
Copy link
Author

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
Copy link
Member

hauner commented Jun 12, 2024

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.

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