Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...