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

Nullable and Non-Nullable versions of a field #4119

Closed
awalsh-broadsoft opened this issue Jan 16, 2018 · 3 comments
Closed

Nullable and Non-Nullable versions of a field #4119

awalsh-broadsoft opened this issue Jan 16, 2018 · 3 comments

Comments

@awalsh-broadsoft
Copy link

Q A
Bug or feature request? Unsure
Which Swagger/OpenAPI version? 3.0.0
Which Swagger-UI version? 3.7
How did you install Swagger-UI? Tomcat
Which browser & version? Chrome
Which operating system? Windows 7 Pro

Demonstration API definition

So previously we were using Json Schema and have recently begun migrating to Open API. There's a particular quirk of our implementation that I'm having trouble porting over.
So what we had sometimes for certain fields would be a non-nullable version of the field (the basic field) and a nullable version of the field. It looked somewhat like this:

"definitions": {
  "DescriptionField": {
    "title": "Book Description",
    "type": "string",
    "maxLength": 10
  },
...
  "NullableDescriptionField": {
    "oneOf": [
      {
        "$ref": "#/definitions/DescriptionField"
      },
      {
        "type": "null"
      }
    ]
  }
}

We're hoping to port this over, but I think the syntax changes enough that we don't have the same functionality.

An implementation I've considered looks like this:

"definitions": {
  "DescriptionField": {
    "title": "Book Description",
    "type": "string",
    "maxLength": 10
  },
...
  "NullableDescriptionField": {
    "allOf": [
      {
        "$ref": "#/definitions/DescriptionField"
      },
      {
        "nullable": true
      }
    ]
  }
}

However, I think there might be a problem with this implementation. I think that because "nullable" defaults to false, that a field couldn't possibly match both schemas, because this would essentially be saying that a matching schema is both nullable = true and nullable = false. Can anyone confirm whether I'm correct in thinking that the above example doesn't work?

Are there any recommendations for how to get nullable versions of fields within Open API 3.0.0?

@hkosova
Copy link
Contributor

hkosova commented Jan 16, 2018

According to the comments here and here, nullable needs to be outside of allOf:

  "NullableDescriptionField": {
    "nullable": true,
    "allOf": [
      {
        "$ref": "#/definitions/DescriptionField"
      }
    ]
  }

@awalsh-broadsoft
Copy link
Author

Thank you. I think that clears things up for me.

@webron
Copy link
Contributor

webron commented Jul 12, 2018

Closing as @hkosova provided the solution.

@webron webron closed this as completed Jul 12, 2018
@lock lock bot locked and limited conversation to collaborators Jul 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants