question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

References are broken for JSON schema (0.4.0)

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
aminalaeecommented, Nov 18, 2021

It’s released now.

1reaction
aminalaeecommented, Nov 18, 2021

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"
                ]
            }
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

schemas with references ($ref) not working and always passing
I have schemas that reference other Ids in the same schema and they always validate, even when they shouldn't.
Read more >
JSON schema validation broken after 5.9.0 - Katalon Community
We're noticing that the ability to do JSON schema validation using using everit's JSON validator referenced here appears to have broken ...
Read more >
Referencing a local relative file in a JSON Schema?
When I don't try to extract MyBool , the schema validates all my JSON samples correctly. The exception I receive is: com.github.fge.jsonschema.
Read more >
Specification - JSON Schema
The meta-schemas are schemas against which other schemas can be validated. It is self-descriptive: the JSON Schema meta-schema validates itself. The latest meta ......
Read more >
CHANGELOG.md - joelittlejohn/jsonschema2pojo
nested sibling references causes error Path not present: definitions only on ... Wrong class name when using $ref in json schema (#164) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found