Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify whether inline and reference attributes are mutually exclusive. Usage of nullable and $ref #2120

Closed
sebastien-rosset opened this issue Jan 30, 2020 · 1 comment

Comments

@sebastien-rosset
Copy link
Contributor

sebastien-rosset commented Jan 30, 2020

I suggest the OAS spec 3.x clarifies, recommends or provides an example for the following scenario: the value of a property is nullable, and the property type is a reference.

Intuitively, one may think about writing:

type: object
properties:
  address:
    $ref: '#/components/schemas/Address'
    nullable: true

The online validator does not complain this is an invalid OAS, but AFAIK this is not compliant with the spec. My interpretation is this snippet is not compliant with the spec because the OAS spec states:

properties - Property definitions MUST be a Schema Object and not a standard JSON Schema (inline or referenced).
Alternatively, any time a Schema Object can be used, a Reference Object can be used in its place. This allows referencing definitions instead of defining them inline.

However, the spec does not explicitly state in unambiguous terms whether the definition is exclusive or not. I.e. is it possible to combine inline with a reference? It's not very clear, so I wouldn't be surprised if tools exhibit inconsistent behavior.

Related to this, in other parts of the spec, there is a reference to the ABNF syntax, and then the OAS uses "|" to represent an alternative. Shouldn't this be replaced with a solidus "/", per RFC 5234?

Since it does not seem legal to mix a $ref with inline attributes (?), I've seen people using "allOf", "anyOf", or "oneOf". They all seem to work in principle, but imo it would be best to have clear guidelines.

type: object
properties:
  address:
    allOf:
     - $ref: '#/components/schemas/Address'
    nullable: true
type: object
properties:
  address:
    anyOf:
     - $ref: '#/components/schemas/Address'
    nullable: true
type: object
properties:
  address:
    oneOf:
     - $ref: '#/components/schemas/Address'
    nullable: true

One consideration is how the tools generate code. Depending on the language of choice, a nullable type can be programmed as a pointer to a struct/class. It would be cumbersome if inline and reference are mutually exclusive, and OAS authors must use anyOf, allOf, or oneOf when they also want to specify nullable=true. If "anyOf/allOf/oneOf" is added, it will either complicate the generated code, or the code generator must be smart enough to simplify the code.

@sebastien-rosset sebastien-rosset changed the title Clarify usage of nullable and $ref Clarify whether inline and reference attributes are mutually exclusive. Usage of nullable and $ref Jan 30, 2020
@sebastien-rosset
Copy link
Contributor Author

I just found out the same discussion thread is at #1368

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant