Use `openapi-spec-validator` for version 2 as well?
Hello!
While testing the latest version of validate_spec on our project, I came across an issue with validating OpenAPI spec v.2. It looks like openapi-spec-validator is only being used for OpenAPI version 3, so prance is falling back to using the default flex validator for version 2.
Something in the default settings in field2property is making the flex validation fail for our OpenAPI v. 2 specs, but if I use openapi-spec-validator, our specs pass validation.
Can we use openapi-spec-validator for versions 2 and 3 in validate_spec? If so, I'm happy to put in a PR (something like this)
Thanks, and please let me know if you have any questions!
Thanks for reporting this @lbeaufort .
According to this, openapi-spec-validator does support both OpenAPI 2.0 and 3.0. The table says that it only supports Python 3, but the package metadata indicates that Python 2 is supported.
I'm open to using openapi-spec-validator for both OAI 2 and 3, but I'd like to understand why the flex validator is failing for default in field2property, as you mentioned. @lbeaufort Would you mind posting an example to reproduce the error?
open-api-spec does support Python2 and in fact we already use it in the Python2 / OpenAPI v3 case. I notified prance about the doc issue: https://github.com/jfinkhaeuser/prance/issues/25
I see no reason not to use it for both OpenAPI versions. I don't think speed matters.
@lbeaufort, it seems we have a potential issue in apispec, and an issue in one of the two validators (flex or open-api-spec). A reproducible test case could help us fix apispec if needed, and would be useful to provide to the faulty validator.
Sounds good! It's been a while since I looked at this, but I can test in the next day or so.
I'm stuck on more specifics, but for now in case it's helpful I saved a text file with our json specs that pass validation with openapi-spec-validator and fail validation with flex. Apologies for the massive file (~3 MB): spec.txt
The file is broken. It contains 5 lines with similar content, but different quotes (single/double). Line 3 looks fine.
I get 3 kinds of validation error, multiple time.
- '/rad-analyst/':
- 'get':
- 'parameters':
- 'parameterObject':
- 'default':
- "The value of `default` must be of one of the declared types for the schema. `None` is not one of `['string']`"
- 'referenceObject':
- 'required':
- '$ref':
- 'This value is required'
- 'additionalProperties':
- "When `additionalProperties` is False, no unspecified properties are allowed. The following unspecified properties were found:\n\t`{'required', 'in', 'description', 'default', 'x-nullable', 'name', 'type'}`"
Regarding the "default" error. AFAIR, nullable is not authorized in OpenAPI v2, it appeared in v3, so we're using x-nullable, which I believe is common. You're setting a default value of None/null, which the validator refuses because it is not of the right type. I don't know if null would be acceptable as a default for a nullable field in OpenAPI v3, but since you're using v2 and x-nullable is not recognized, the parser cannot know null is acceptable anyway.
This error seems legit. I don't think this is something that should be addressed in apispec, though. This is user input. I'd say flex does a better job, here.
I don't understand the two other errors.
Here's the line where openapi-spec-validator skips default if it is None: https://github.com/p1c2u/openapi-spec-validator/blob/e41c77221368351021c7ce4442c7050ca1e7ffa6/openapi_spec_validator/validators.py#L305. Should this test be removed?
Here's a "minified" spec file to reproduce:
{"info": {"title": "OpenFEC", "version": "1.0"}, "paths": {"/rad-analyst/": {"get": {"tags": ["filer resources"], "responses": {"default": {"schema": {"$ref": "#/definitions/RadAnalystPage"}, "description": ""}}, "parameters": [{"in": "query", "name": "min_assignment_update_date", "required": false, "type": "string", "format": "date", "description": "Filter results for assignment updates made after this date"}, {"in": "query", "name": "sort", "required": false, "type": "string", "default": null, "x-nullable": true, "description": "Provide a field to sort by. Use - for descending order."}, {"in": "query", "name": "email", "required": false, "collectionFormat": "multi", "type": "array", "items": {"type": "string"}, "description": "Email of RAD analyst"}, {"in": "query", "name": "page", "required": false, "type": "integer", "format": "int32", "default": 1, "description": "For paginating through results, starting at page 1"}, {"in": "query", "name": "sort_hide_null", "required": false, "type": "boolean", "default": false, "description": "Hide null values on sorted column(s)."}, {"in": "query", "name": "telephone_ext", "required": false, "collectionFormat": "multi", "type": "array", "items": {"type": "integer", "format": "int32"}, "description": "Telephone extension of RAD analyst"}, {"in": "query", "name": "max_assignment_update_date", "required": false, "type": "string", "format": "date", "description": "Filter results for assignment updates made before this date"}, {"in": "query", "name": "sort_null_only", "required": false, "type": "boolean", "default": false, "description": "Toggle that filters out all rows having sort column that is non-null"}, {"in": "query", "name": "title", "required": false, "collectionFormat": "multi", "type": "array", "items": {"type": "string"}, "description": "Title of RAD analyst"}, {"in": "query", "name": "per_page", "required": false, "type": "integer", "format": "int32", "default": 20, "description": "The number of results returned per page. Defaults to 20."}, {"in": "query", "name": "analyst_short_id", "required": false, "collectionFormat": "multi", "type": "array", "items": {"type": "integer", "format": "int32"}, "description": "Short ID of RAD analyst"}, {"in": "query", "name": "committee_id", "required": false, "collectionFormat": "multi", "type": "array", "items": {"type": "string"}, "description": "\nA unique identifier assigned to each committee or filer registered with the FEC. In general committee id's begin with the letter C which is followed by eight digits.\n"}, {"in": "query", "name": "name", "required": false, "collectionFormat": "multi", "type": "array", "items": {"type": "string"}, "description": "Name of RAD analyst"}, {"in": "query", "name": "analyst_id", "required": false, "collectionFormat": "multi", "type": "array", "items": {"type": "integer", "format": "int32"}, "description": "ID of RAD analyst"}]}}}, "tags": [{"name": "filer resources", "description": "Useful tools for those who file with the FEC. \n\n Look up RAD analyst with telephone extension by committee_id."}], "swagger": "2.0", "definitions": {"OffsetInfo": {"type": "object", "properties": {"count": {"type": "integer", "format": "int32"}, "per_page": {"type": "integer", "format": "int32"}, "page": {"type": "integer", "format": "int32"}, "pages": {"type": "integer", "format": "int32"}}}, "RadAnalyst": {"type": "object", "properties": {"rad_branch": {"type": "string", "x-nullable": true, "maxLength": 100, "description": "Branch of RAD analyst"}, "assignment_update_date": {"type": "string", "format": "date", "x-nullable": true, "description": "Date of most recent RAD analyst assignment change"}, "analyst_email": {"type": "string", "x-nullable": true, "maxLength": 100, "description": "Email of RAD analyst"}, "first_name": {"type": "string", "x-nullable": true, "maxLength": 255, "description": "Fist name of RAD analyst"}, "telephone_ext": {"type": "number", "x-nullable": true, "description": "Telephone extension of RAD analyst"}, "committee_name": {"type": "string", "x-nullable": true, "maxLength": 100, "description": "The name of the committee. If a committee changes its name, the most recent name will be shown. Committee names are not unique. Use committee_id for looking up records."}, "analyst_title": {"type": "string", "x-nullable": true, "maxLength": 100, "description": "Title of RAD analyst"}, "last_name": {"type": "string", "x-nullable": true, "maxLength": 100, "description": "Last name of RAD analyst"}, "analyst_short_id": {"type": "number", "x-nullable": true, "description": "Short ID of RAD analyst."}, "committee_id": {"type": "string", "description": "\nA unique identifier assigned to each committee or filer registered with the FEC. In general committee id's begin with the letter C which is followed by eight digits.\n"}, "analyst_id": {"type": "number", "x-nullable": true, "description": "ID of RAD analyst."}}, "required": ["committee_id"]}, "RadAnalystPage": {"type": "object", "properties": {"pagination": {"$ref": "#/definitions/OffsetInfo"}, "results": {"type": "array", "items": {"$ref": "#/definitions/RadAnalyst"}}}}}, "parameters": {}, "basePath": "/v1", "produces": ["application/json"], "securityDefinitions": {"apiKey": {"type": "apiKey", "name": "api_key", "in": "query"}}, "security": [{"apiKey": []}]}
Apologies for my lack of expertise on these matters.
In regards to validating the default values for optional fields (openapi-spec-validator skips default if it is None) - how could we write specs for a non-required field if a default of none/null isn't permitted? Sorry if I'm missing something.
Looks like you wrote
key = fields.Field(allow_none=True, default=None, required=False)
If the field is not required, no need to set a default and no need to use allow_none. (There is generally no need to use allow_none. Using allow_none in a REST API context is asking for trouble from my experience. Think twice before doing so.)
key = fields.Field(required=False)
If the client sends no value, then nothing happens. The deserialized data does not contain the key.
If the client sends null then validation fails as it is not a valid value.
I hope this helps.
Playing around with the minified spec file (thank you) - it looks like the behavior is the same (I used https://editor.swagger.io/) whether "default": null is set or skipped. I'm going to try skipping it on our end and see if that addresses the validation issues. I really appreciate your help!
^^ I see you came to the same conclusion, excellent, thanks again! Sorry I didn't see your message sooner!
@lbeaufort Can this issue be closed?
@sloria unfortunately I'm having issues with the validate_spec using flex on our end, and we haven't had the resources to iron them out yet.
After following the discussion here: https://github.com/marshmallow-code/apispec/pull/299, I may implement our own validate_spec using prance and openapi-spec-validator because flex seems overly restrictive for our project (our specs are live and there aren't any issues).
I appreciate all your help on this! I think it's ok to close this issue.