Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nullable does not get converted correctly #24

Open
webmaster777 opened this issue Apr 25, 2019 · 1 comment
Open

nullable does not get converted correctly #24

webmaster777 opened this issue Apr 25, 2019 · 1 comment

Comments

@webmaster777
Copy link

in a swagger 2.0 yaml file:

# some property in a definition
      datumStart:
        type: string
        format: date
        example: '2019-01-13'
        nullable: true

when converted to schema:

Actual output

    "datumStart": {
      "nullable": true, 
      "type": "string", 
      "example": "2019-01-13", 
      "format": "date"
    }, 

Expected output

    "datumStart": {
      "type": ["string", "null"], 
      "example": "2019-01-13", 
      "format": "date"
    }, 
@mzsombor
Copy link

Although my use case might be different than yous I still think it could be useful to share my results. I recently started to use swaggerhub (openapi3) for api definition. I was impressed of how useful this tool can be, but when it comes to data validation in python things become tricky. According to https://openapi.tools/ there is no data validation support in python so I started to look for alternatives and this is how I ended up using openapi2jsonschema but ran into the same "nullable not being converted" issue.

After several hours spent on looking for different projects I found https://github.com/p1c2u/openapi-core and an issue regarding data validation in python: python-openapi/openapi-core#154 . Using the example from this issue I managed to write a simple python3 script to validate data against an openapi3 schema.

from jsonschema.validators import RefResolver
from openapi_spec_validator import default_handlers
from openapi_spec_validator.validators import Dereferencer
from openapi_core.schema.schemas.registries import SchemaRegistry

schema1 ={
    "openapi": "3.0.0",
    "info": { "title": "", "version": "0.1" },
    "paths": None,
    "components": {
        "schemas": {
            "test": {
                "type": "string",
                "pattern": "test"
            }
        }
    }
}

schema2 = {
    "$ref": "#/components/schemas/test"
}

resolver = RefResolver.from_schema(schema1, handlers=default_handlers)
dereferencer = Dereferencer(resolver)
schema_registry = SchemaRegistry(dereferencer)
schema, here = schema_registry.get_or_create(schema2)

schema.validate("tes")

I'm not familiar with the libraries that are imported but you can see that jsonschema is still used so you might be able to extract your jsonschema data from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants