Freeform subobject in json-schema

2019-03-06 11:19发布

问题:

I am drafting an API documentation with swagger.io and is trying to make it fit to our use case. The system is going to receive and process data from all sources, and they would each have different sets of fields.

While the product of the processing share the same schema, we want to include the input in the schema too for reference purpose. For instance, given

{
    "foo": "bar"
    "bar": "baz"
}

The product of the processing is

{
    "original": {
        "foo": "bar",
        "bar": "baz"
    }
    "processed": {
        "stdFieldA": "bar",
        "stdFieldB": "baz"
    }
}

Assuming for each input from different sources, we end up having stdFieldA and stdFieldB. So the response schema object we have

type: object
properties:
    processed:
        type: object
        properties:
            stdFieldA:
                type: string
            stdFieldB:
                type: string

now that we have the processed subobject defined, can we define a freeform object for the original input, so that this object coming from another source is valid

{
    "alpha": "lorem",
    "beta": "ipsum"
}

If I don't get any answer to this, my workaround to the problem would be storing the original input as string (convert the original input into JSON string).

回答1:

type: object without properties describes a free-form object. So the response schema can be:

type: object
properties:
    original:
        type: object
    processed:
        type: object
        properties:
            stdFieldA:
                type: string
            stdFieldB:
                type: string