Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

jsonschema - Freeform subobject in json-schema

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...