Skip to content

openapi validation question #95

Open

Description

@matteo-rama

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

Activity

  1. hauner commented on Jun 7, 2024

    @hauner
    Member

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

  2. hauner commented on Jun 8, 2024

    @hauner
    Member

    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("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/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))
            }
        }
    )}
  3. matteo-rama commented on Jun 10, 2024

    @matteo-rama
    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);
    
  4. hauner commented on Jun 11, 2024

    @hauner
    Member

    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.

  5. matteo-rama commented on Jun 11, 2024

    @matteo-rama
    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

  6. hauner commented on Jun 12, 2024

    @hauner
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions