fuhrysteve/marshmallow-jsonschema

Support for using load_from or dump_to as field titles

smith-m opened this issue · 2 comments

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"
}

Nice catch!

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:

properties[field.name] = schema

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