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

References are broken for JSON schema (0.4.0) #121

Closed
2 tasks done
as-dg opened this issue Nov 15, 2021 · 5 comments · Fixed by #122
Closed
2 tasks done

References are broken for JSON schema (0.4.0) #121

as-dg opened this issue Nov 15, 2021 · 5 comments · Fixed by #122
Labels

Comments

@as-dg
Copy link

as-dg commented Nov 15, 2021

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

Quoting the following change from the changelog for 0.4.0:

Update JSON schema ref according to OAS 3: #/components/schemas/

Currently I'm facing the issue that when a typesystem schema contains a reference, it will not be resolvable in the resulting JSON Schema.
I made an example based on https://github.com/encode/typesystem/blob/master/docs/references.md.

To reproduce

import json
import typesystem

definitions = typesystem.Definitions()

artist_schema = typesystem.Schema(fields={"name": typesystem.String(max_length=100)})

album_schema = typesystem.Schema(
    fields={
        "title": typesystem.String(max_length=100),
        "release_date": typesystem.Date(),
        "artist": typesystem.Reference(to="Artist", definitions=definitions),
    }
)

definitions["Artist"] = artist_schema
definitions["Album"] = album_schema

document = typesystem.to_json_schema(album_schema)
print(json.dumps(document, indent=4))

I'm simply trying to get a JSON Schema out of the defined album_schema.

Expected behavior

Receive a JSON Schema output that contains the correct $ref which makes it resolvable.

Actual behavior

JSON Schema output that contains a definitions key which is not in line with the new syntax (i.e.: "$ref": "#/components/schemas/Artist")

{
    "type": "object",
    "properties": {
        "title": {
            "type": "string",
            "minLength": 1,
            "maxLength": 100
        },
        "release_date": {
            "type": "string",
            "minLength": 1,
            "format": "date"
        },
        "artist": {
            "$ref": "#/components/schemas/Artist"
        }
    },
    "required": [
        "title",
        "release_date",
        "artist"
    ],
    "definitions": {
        "Artist": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 100
                }
            },
            "required": [
                "name"
            ]
        }
    }
}

With the newly generated reference syntax ("#/components/schemas/Artist"), shouldn't the document contain the elements "components" as well as "schemas" and respectively "Artist" in this example?

Environment

  • OS: macOS
  • Python version: 3.9.6
  • Typesystem version: 0.4.0
@aminalaee
Copy link
Member

Thank you for reporting this. Yes, you are right.

it should be something like this:

...
"components": {"schemas": "Artist": ....}

I noticed that a while ago but I've been busy. Feel free to create a PR for this.

@aminalaee
Copy link
Member

The output will be like this now:

{
    "type": "object",
    "properties": {
        "title": {
            "type": "string",
            "minLength": 1,
            "maxLength": 100
        },
        "release_date": {
            "type": "string",
            "minLength": 1,
            "format": "date"
        },
        "artist": {
            "$ref": "#/components/schemas/Artist"
        }
    },
    "required": [
        "title",
        "release_date",
        "artist"
    ],
    "components": {
        "schemas": {
            "Artist": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 100
                    }
                },
                "required": [
                    "name"
                ]
            }
        }
    }
}

@as-dg
Copy link
Author

as-dg commented Nov 18, 2021

Awesome, thanks for the quick response @aminalaee! Just plugged the branch into my project and can confirm it's working as expected now. Hoping for a release soon. 😄

@aminalaee
Copy link
Member

Thank you for reporting this 👍
Sure, I'll try to release 0.4.1 today.

@aminalaee
Copy link
Member

It's released now.

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

Successfully merging a pull request may close this issue.

2 participants