fuhrysteve / marshmallow-jsonschema

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

Support for using load_from or dump_to as field titles #64

Open smith-m opened 6 years ago

smith-m commented 6 years ago

JSONSchema should support to use of load_from or dump_to (or the equivalent in marshmallow 3)

for example:

class CampaignCompletion(Schema):

    id_ = fields.UUID(load_from='id', dump_to='id') 
    version = fields.Integer()
    timestamp = fields.DateTime()
    campaign_name = fields.String(load_from='campaignName', dump_to='campaignName')

Currently this dumps to

{
    "definitions": {
        "CampaignCompletion": {
            "properties": {
                "campaign_name": {
                    "title": "campaign_name",
                    "type": "string"
                },
                "id_": {
                    "title": "id_",
                    "type": "string",
                    "format": "uuid"
                },
                "timestamp": {
                    "title": "timestamp",
                    "type": "string",
                    "format": "date-time"
                },
                "version": {
                    "title": "version",
                    "type": "number",
                    "format": "integer"
                }
            },
            "type": "object",
            "required": []
        }
    },
    "$ref": "#/definitions/CampaignCompletion"
}

In this case, the correct json schema of our model is the one that represents the serialized object, not the python dictionary/object behind the schema. So

{
    "definitions": {
        "CampaignCompletion": {
            "properties": {
                "campaignName": {
                    "title": "campaignName",
                    "type": "string"
                },
                "id": {
                    "title": "id",
                    "type": "string",
                    "format": "uuid"
                },
                "timestamp": {
                    "title": "timestamp",
                    "type": "string",
                    "format": "date-time"
                },
                "version": {
                    "title": "version",
                    "type": "number",
                    "format": "integer"
                }
            },
            "type": "object",
            "required": []
        }
    },
    "$ref": "#/definitions/CampaignCompletion"
}
fuhrysteve commented 6 years ago

Nice catch!

fuhrysteve commented 6 years ago

I'm afraid I won't be able to get to it for quite some time.

But in case you or anyone else are inclined to take a stab at fixing it, I suspect the solution is changing our usage of field.name to something else that includes load_from and all that:

https://github.com/fuhrysteve/marshmallow-jsonschema/blob/3e0891a79d586c49deb75188d9ee1728597d093b/marshmallow_jsonschema/base.py#L115

I'd be happy to review and merge any pull requests w/ some tests to prove it works!