Freeform subobject in json-schema

泄露秘密 提交于 2019-12-29 10:05:23

问题


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


来源:https://stackoverflow.com/questions/41606152/freeform-subobject-in-json-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!