Swagger Yaml schema definition for object without a fixed property list

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I am implementing an API-first application with the help of Swagger. One of the most important objects to be returned is a DICOM object, which returns a collection of attributes with flexible names, for example:

{    "00080005": {"vr":"CS","Value":["ISO_IR 100"]},   "00080020": {"vr":"DA","Value":["20160602"]},   "00080030": {"vr":"TM","Value":["171855.7490"]},   "00080050": {"vr":"SH","Value":["1234"]},   "00080090": {"vr":"PN","Value":[{"Alphabetic":"Parikh MD^Anush^M"}]} } 

So I cannot know the name of all the attributes in advance (00080005, 00080030, etc.) although the file structure is very uniform.

My concrete question is: what would be the schema definition for such JSON document.

I have tried the following without success:

definitions:   DicomMetadataJson:     type: object     patternProperties:       ^\d{8}:         type: object       

but the Swagger Editor returns an error like:

code: "OBJECT_ADDITIONAL_PROPERTIES"

message: "Additional properties not allowed: patternProperties"

description: "A deterministic version of a JSON Schema object."

回答1:

OpenAPI (fka. Swagger) use only a subset of JSON Schema v4 which, unfortunately, do not propose patternProperties.

But given the provided example is a map, you can describe it using additionalProperties:

swagger: "2.0" info:   version: 1.0.0   title: Hashmap  paths: {}  definitions:   DicomMetadataJson:     additionalProperties:       properties:         vr:           type: string         Value:           type: array           items:             type: string 

The key is not defined and is supposed to be a string (therefore you cannot enforce it's format).

Note that SwaggerUI Swagger UI do not render them for now.The issue is tracked here https://github.com/swagger-api/swagger-ui/issues/1248

In the meanwhile you can use this trick define a non required property (default in the example below) of the same type of the map's objects and give some hint within the description:

swagger: "2.0" info:   version: 1.0.0   title: Hashmap  paths: {}  definitions:   MetaData:     properties:       vr:         type: string       Value:         type: array         items:           type: string    DicomMetadataJson:     description: 'A <string,MetaData> map, default key is only for documentation purpose'     properties:       default:         $ref: '#/definitions/MetaData'     additionalProperties:       $ref: '#/definitions/MetaData' 

Concerning the quote The schema exposes two types of fields. Fixed fields, which have a declared name, and Patterned fields, which declare a regex pattern for the field name. Patterned fields can have multiple occurrences as long as each has a unique name., it concerns the format of the OpenAPI specification itself and not the objects used by the API described with an OpenAPI specification.



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