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

Doubly nested list schema generates null title #87

Open
yeralin opened this issue Aug 19, 2019 · 3 comments
Open

Doubly nested list schema generates null title #87

yeralin opened this issue Aug 19, 2019 · 3 comments

Comments

@yeralin
Copy link
Contributor

yeralin commented Aug 19, 2019

Singly nested schema list like:

class TestSchema(Schema):
    test = List(Integer()))

Works just fine:

{
    "$ref": "#/definitions/TestSchema",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "TestSchema": {
            "additionalProperties": false,
            "properties": {
                "test": {
                    "items": {
                        "format": "integer",
                        "title": "test", <- title is assigned w/o any problems
                        "type": "number"
                    },
                    "title": "test",
                    "type": "array"
                }
            },
            "type": "object"
        }
    }
}

But if you add another level of nestness, it will start generating null values for titles which doesn't conform to JSON Schema specifications. To reproduce:

from marshmallow import Schema
from marshmallow.fields import List, Integer
from marshmallow_jsonschema import JSONSchema
import json

class TestSchema(Schema):
    test = List(List(Integer()))

test_schema = TestSchema()
json_schema = JSONSchema()
print(json.dumps(json_schema.dump(test_schema).data, indent=4))

Will yield:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "TestSchema": {
            "type": "object",
            "properties": {
                "test": {
                    "title": "test",
                    "type": "array",
                    "items": {
                        "title": "test",
                        "type": "array",
                        "items": {
                            "title": null, <- problem
                            "type": "number",
                            "format": "integer"
                        }
                    }
                }
            },
            "additionalProperties": false
        }
    },
    "$ref": "#/definitions/TestSchema"
}
@yeralin
Copy link
Contributor Author

yeralin commented Aug 19, 2019

Actually, it might be a problem in marshmallow package, when I try to resolve:
test_schema.fields['test'].container.container.name it returns None (on doubly nested schema) vs. when I call test_schema.fields['test'].container.name, it returns test (on singly nested schema).

Raised an issue under marhsmallow core repo: marshmallow-code/marshmallow#1355

@yeralin
Copy link
Contributor Author

yeralin commented Aug 20, 2019

Ok, after the investigation of this issue under the marshmallow core repo, it was discovered that this bug was fixed in 3.0, but not in 2.0! And they can't fix it in 2.0 bc it will not be backwards compatible...

Then, their maintainer added:

As far as #87 is concerned, I would recommend checking for None and omitting the title. I don't think title's are require for json-schema fields and they don't seem to provide any useful context in this situation.

What do you think? @fuhrysteve

@yeralin
Copy link
Contributor Author

yeralin commented Oct 3, 2019

How about this: completely skip title (since it is not required by JSON Schema specification) if either field.attribute or field.name are None?

Created a PR

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

1 participant