Yelp / swagger_spec_validator

Other
104 stars 71 forks source link

Validation fails if definition has `required` but uses `allOf` #66

Closed infinitewarp closed 7 years ago

infinitewarp commented 7 years ago

I'm trying to use connexion which wraps and adds functionality over swagger-spec-validator, but the schema definition I'm trying to use fails to validate. I've narrowed the problem to a specific scenario. Whenever a definition has a required list but defines its properties nested in allOf instead on the peer-level properties, schema validation raises a SwaggerValidationError.

For example, using this schema:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "test"
    },
    "paths": {},
    "definitions": {
        "User": {
            "type": "object",
            "required": [
                "id"
            ],
            "allOf": [
                {
                    "$ref": "#/definitions/CommonBaseType"
                },
                {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string"
                        },
                        "name": {
                            "type": "string"
                        }
                    }
                }
            ]
        },
        "CommonBaseType": {
            "type": "object",
            "properties": {
                "commonBaseThing": {
                    "type": "string"
                }
            }
        }
    }
}

I get SwaggerValidationError: Required list has properties not defined: [u'id'].

The schema appears to be valid (it was actually generated by another swagger framework in an unrelated system). Or am I misunderstanding something about the expected behavior?

I think I've fixed it in https://github.com/Yelp/swagger_spec_validator/pull/65, but I'm not intimately familiar with the internals of this particular module and would gladly welcome some feedback.

crochik commented 7 years ago

see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#models-with-composition

sjaensch commented 7 years ago

Your required section needs to be within the object definition that defines that field. Check your linked example again, that's how they do it as well. Please reopen the issue if you're still encountering problems after doing the change.

infinitewarp commented 7 years ago

Thanks for the advice, @sjaensch ! Your are correct, and when we adjusted our swagger.json-generator to move the required section as you described, I was successfully able to validate it.