fuhrysteve / marshmallow-jsonschema

JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow
MIT License
209 stars 72 forks source link

Doubly nested list schema generates null title #87

Open yeralin opened 5 years ago

yeralin commented 5 years ago

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 commented 5 years ago

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: https://github.com/marshmallow-code/marshmallow/issues/1355

yeralin commented 5 years ago

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 fuhrysteve/marshmallow-jsonschema#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 commented 5 years ago

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