OpenAPITools / OpenAPITools/openapi-generator
[Clarification Needed] Validation and code generation of open-ended "type: object"
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
OpenAPI declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the python-experimental and go-experimental generators using the open-ended type: object schema shown in the issue. Check how each generator treats object, null, primitive, and array values, then consult the referenced JSON Schema and OpenAPI rules. Done means the expected semantics are decided and the affected generator behavior is documented or aligned.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100