[Clarification Needed] Validation and code generation of open-ended "type: object"
Created by: sebastien-rosset
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
According to the JSON schema spec, when a schema is an open-ended "type: object", such as below, the document must be an unordered set of properties mapping a string to an instance, from the JSON "object" production.
components:
schemas:
Blob:
type: object
In the above example, additionalProperties is not specified, hence it has the default value "true"
AFAIK, that means the document cannot be the "null" value, it cannot be a primitive type (e.g. string, integer...) and it cannot be an array. Is my understanding correct? If on the other hand, the intent is the document can take any valid JSON value, then with the JSON schema specification, and putting the OAS spec aside for a moment, it could be specified with a type array:
type: [ 'null', object, array, boolean, string, number ]
But type arrays are not supported in OAS, so instead my question is are we supposed to use "oneOf"?
components:
schemas:
Blob:
nullable: true # cannot use type: 'null' in OAS 3.0
oneOf:
- type: object
- type: array
- type: boolean
- type: string
- type: number
But in practice, I see multiple generators are lenient for "type: object" and accept any valid JSON document. Even if we use "oneOf" in the spec, multiple generators do not support 'oneOf' very well, so I am reluctant to use that construct for this specific purpose (because I need to generate SDK that work for multiple languages). I haven't gone through the entire list, but I see at least two different behaviors across two generators. For example, "python-experimental" accepts any JSON valid document:
'blob': (bool, date, datetime, dict, float, int, list, str, none_type,), # noqa: E501
But with the same OAS spec, go-experimental implements a map[string]interface{}, which means the value can either be the nil value or a map of string to any value; with strict validation, it would have to be written as:
type: [ 'null', object ]
openapi-generator version
master February 20th 2020