Git Product home page Git Product logo

Comments (8)

ngnpope avatar ngnpope commented on June 18, 2024 1

I have also bumped into this issue. I believe that these lines are incorrect and should be removed:

https://github.com/p1c2u/openapi-schema-validator/blob/ab7222af0597ebc672f93cc7f7e089897aba1bec/openapi_schema_validator/validators.py#L68-L72

This is for a number of reasons, all of which have affected me:

  • OpenAPI 3.0.x should ignore siblings of $ref (1)
  • OpenAPI 3.0.x needs special hacks for nullable with enum (1, 2, 3)
  • This breaks nullable set to True alongside allOf/anyOf/oneOf. (This is your case @knivets.)

The comment # append defaults to trigger validator (i.e. nullable) doesn't seem to make sense as validation seems to work perfectly fine without it. Perhaps @p1c2u can shed some light on why this is/was necessary?

This shouldn't be an issue with OpenAPI 3.1.x when it is supported as null is allowed as a type.

I have used the following workaround which may help:

from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
from openapi_core.unmarshalling.schemas.factories import SchemaUnmarshallersFactory
from openapi_core.validation.response.validators import ResponseValidator
from openapi_schema_validator import OAS30Validator

class FixedOAS30Validator(OAS30Validator):
    def iter_errors(self, instance, _schema=None):
        # Use the implementation of .iter_errors() from the parent.
        return super(OAS30Validator, self).iter_errors(instance, _schema)

class FixedSchemaUnmarshallersFactory(SchemaUnmarshallersFactory):
    def get_validator(self, schema):
        kwargs = {"resolver": self.resolver, "format_checker": self.format_checker}
        if self.context is not None:
            kwargs[self.CONTEXT_VALIDATION[self.context]] = True
        with schema.open() as schema_dict:
            return FixedOAS30Validator(schema_dict, **kwargs)

class FixedResponseValidator(ResponseValidator):
    @property
    def schema_unmarshallers_factory(self):
        return FixedSchemaUnmarshallersFactory(
            self.spec.accessor.dereferencer.resolver_manager.resolver,
            self.format_checker,
            self.custom_formatters,
            context=UnmarshalContext.RESPONSE,
        )

from openapi-schema-validator.

claeyswo avatar claeyswo commented on June 18, 2024

We are experiencing the same issue. As the previous comment stated: Perhaps @p1c2u can shed some light on why this is/was necessary?

from openapi-schema-validator.

p1c2u avatar p1c2u commented on June 18, 2024

That's exactly what comment says to trigger nullable validator.

The comment # append defaults to trigger validator (i.e. nullable) doesn't seem to make sense as validation seems to work perfectly fine without it.

It can work fine if you define nullable param explicitly.
For schema without nullable defined (default is False) if you pass None value it won't raise validation error - which is incorrect. You can try to remove these lines and run tests.

from openapi-schema-validator.

ngnpope avatar ngnpope commented on June 18, 2024

Maybe the issue I was bumping into was related to #24 (solved by #26), but I haven't been able to check.

from openapi-schema-validator.

p1c2u avatar p1c2u commented on June 18, 2024

Same here, can't reproduce the issue. @claeyswo which jsonschema version you use and do you have any example of the issue?

from openapi-schema-validator.

claeyswo avatar claeyswo commented on June 18, 2024

3.0, but by adding nullable to every item the problem was solved. By switching the nullable om some_id in the example below we get the desired effect.

schema = {
    "$ref": "#/components/schemas/TestSchema",
    "components": {
        "schemas": {
            "IntegerString": {
                "anyOf": [
                    {"type": "integer", "nullable": True},
                    {"type": "string", "nullable": True},
                ],
                "nullable": True,
            },
            "TestSchema": {
                "type": "object",
                "properties": {
                    "some_id": {
                        "$ref":  "#/components/schemas/IntegerString",
                        "nullable": True,
                    }
                },
            },
        }
    },
}

from openapi-schema-validator.

ghandic avatar ghandic commented on June 18, 2024
attribute:
    nullable: true
    $ref: '#/components/Example'
attribute:
    oneOf:
        - type: "null"
        - $ref: '#/components/Example'

Neither work, any update on a fix for this?

from openapi-schema-validator.

p1c2u avatar p1c2u commented on June 18, 2024

There were ambiguities in the semantics of nullable in OAS 3.0.0-2 More about the issue can be found in the following proposal. OAS 3.0.3 clarified the definition so it can be implemented properly. There shouldn't be any major backward compatibility issues.

from openapi-schema-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.