question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reference objects don't combine well with “nullable”

See original GitHub issue

Perhaps more of a question than an issue report, but I am not sure how to combine “nullable” with a reference object; I have the impression they don’t combine well?

In Swagger-UI issue 3325 I posted an example with the following schema. The intent is to express that both “dataFork” and “resourceFork” contain a Fork, except that “resourceFork” can also be null, but “dataFork” cannot be null. As was pointed out though, the schema is not in accordance with the OpenAPI v3.0.0 specification, due to the use of JSON Schema’s {"type":"null"} which has not been adopted in OpenAPI.

...
"File": {
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "dataFork": { "$ref": "#/components/schemas/Fork" },
    "resourceFork": {
      "anyOf": [
        { "type": "null" },
        {"$ref": "#/components/schemas/Fork" } ] },
    ...

I considered I can instead express the example as follows, which as far as I can tell is at least in accordance with the OpenAPI specification? But the use of “anyOf” with only one schema inside seems clumsy, is there a better way to handle this? Essentially, a better way to combine “nullable” with the reference object?

... 
"File": {
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "dataFork": { "$ref": "#/components/schemas/Fork" },
    "resourceFork": {
      "nullable": true,
      "anyOf": [
        { "$ref": "#/components/schemas/Fork" } ] },
    ...

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:31
  • Comments:39 (26 by maintainers)

github_iconTop GitHub Comments

3reactions
philsturgeoncommented, Sep 7, 2020

A is not valid OpenAPI v3.0, but would be valid OpenAPI v3.1 as it has aligned with JSON Schema 2019-09.

I’ve never seen B, feels funny. Don’t have all the answers for this. Most folks do the allOf thing but that is apparently incorrect and always has been, despite me doing it for bloomin ages in various tools which seemed to think it was just fine.

2reactions
sebastien-rossetcommented, Jan 30, 2020

@sebastien-rosset , if you have not done so already, please have a look at the proposal to clarify nullable:

Proposal: Clarify Semantics of nullable in OpenAPI 3.0

Yes, I have read it. One reason I am advocating for examples, is that without them, it’s pretty challenging to understand what is valid in each of these specs. You have to read OAS 3.1.0, 3.0.2 and the JSON schema, carefully parse some of the crucial sentences and compare them. I think all of this confusion can go away with a few good examples.

Read more comments on GitHub >

github_iconTop Results From Across the Web

openapi - How to specify a property can be null or a reference ...
This is further discussed in the OpenAPI Specification repository: Reference objects don't combine well with “nullable”.
Read more >
C# 8.0 nullable references and serialization - Endjin
C# 8's nullable references feature can clash with common serialization techniques. This article shows how to deal with that.
Read more >
Fighting with nullable reference types in Razor Pages
In this post I discuss C#'s nullable reference types in the context of Razor Pages, why they don't play nicely together and how...
Read more >
Nulls in Value Objects - Enterprise Craftsmanship
If Nullable<T> makes sense in the context of the ValueObject , then choose the first approach ( ValueObject<Nullable<T>> ); otherwise, go with ...
Read more >
Embracing nullable reference types - Microsoft Developer Blogs
Remind me – what is this feature again? ; string variable ; s because it might be null! Ordinary reference types have become...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found