Open sanderegg opened 6 years ago
@sanderegg hi. It is possible to use external files. You need to pass additional parameter spec_url='file:///path/to/your/openapi.yaml'
when creating spec.
Your test.json is not valid because there is no null type in OpenAPI 3. See nullable
Hi @p1c2u , Yes I did this, I actually realise I forgot to show the python code:
from openapi_spec_validator import validate_spec, factories
from openapi_core import create_spec
from openapi_core.shortcuts import RequestValidator
from pathlib import Path
import yaml
import json
SPEC_FILE = Path(__file__).parent / "testapi.yaml"
with SPEC_FILE.open() as fp:
specification_dict = yaml.load(fp)
uri = SPEC_FILE.as_uri()
try:
# factories.config
spec = create_spec(specification_dict, spec_url=uri)
validator = RequestValidator(spec)
except Exception as err:
print(err)
The element of the document located on the same server should look like follow – $ref: 'test.json'
I tried $ref: 'test.json'
and $ref: './test.json'
both return the same error as explained above.
The latter works better in some other packages.
Actually the problem is not in finding the file, this works. The problem is I think that it is not looking like:
{'oneOf': [{'$ref': '#/definitions/response'},
{'$ref': '#/definitions/reference'}]}
@sanderegg it works for me after fixing `null issue
f = open('testapi.yaml')
spec_dict = safe_load(f)
spec = create_spec(spec_dict, spec_url='file:///path/to/file/test.yaml')
@p1c2u what do you mean fixing `null issue?
@sanderegg see my first comment
so you changed to
// test.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Test",
"type": ["string"],
"format": "date-time"
}
is that correct? cause for me I still get the very same error .
There is no such keyword as $schema
and type
should be string
Here is valid:
// test.json
{
"title": "Test",
"type": "string",
"format": "date-time",
"nullable": true
}
Ok, but then it is not anymore a json-schema but a openapi. So it is not possible to mix them together in that case.
Hmm. What can I say more. It's OpenAPI library and OpenAPI schema is subset of JSONSchema
The Schema Object allows the definition of input and output data types. This object is an extended subset of the JSON Schema Specification Wright Draft 00.
ok, so after hours of research it's actually possible folowing: this and this.
So, using a json-schema file, passing it through the json-schema-to-openapi converter and then in the openapi-validator. The node command line tool speccy actually validates an openapi with integrated json-schema inside using the -j flag.
Thanks for the info. It's a bit hacky. "More Proper Solution" looks good. Now we need to wait for OpenAPI 3.1 release.
What is the status of this issue? The incompatibility with JSONSchema is a disaster, especially with more complicated Schemata (with internal or external references).
Hello,
I am currently in the process of creating a python based REST api. I wrote an openapi 3.0 interface that reference an external json-schema file using $ref: 'test.json' (openapiref)[https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject] as I thought this is supposed to be possible.
Now I wanted to use openapi-core to validate requests and responses but I hit a problem which is that the validation of my openapi interface file does not go through because I get an error of type:
my example openapi interface looks like this:
and my example json-schema:
Now I understand the problem comes from factories.py:SpecFactory:create where openapi_spec_validator is failing in validating my file because the ref does not follow #/something.
Am I wrong in thinking I can direclty reference a JSON schema? If not, is it possible to disable the spec validation from client side? Or should I change something in the way I reference it?
Thanks for any input on this...