How to specify a property can be null or a reference

3,865 views
Skip to first unread message

Daniel Popowich

unread,
Dec 1, 2016, 5:38:59 PM12/1/16
to Swagger
I've seen a lot of talk on github issues regarding swagger's lack of support for oneOf and anyOf. Setting aside the merits of the arguments over there, I'm looking for a practical solution to the problem of a response object (or any object, really) that has a property that can be EITHER another object, or null.  This is a real-world problem for my project: complex objects reference other objects, but in some cases, the referenced object is NULL, i.e, some parent objects do not point to a child and null is the appropriate value in these cases.

I'm surprised the spec doesn't handle this readily.  There is x-nullable, provided by some consumers of the spec, but that does not work in conjunction with $ref, where the reference replaces the object wholesale, leaving the x-nullable setting ignored.

I think I asked the question well over on stackoverflow, maybe someone here can earn some points over there:

http://stackoverflow.com/questions/40920441/how-to-specify-a-property-can-be-null-or-a-reference-with-swagger

tony tam

unread,
Dec 2, 2016, 12:05:24 PM12/2/16
to swagger-sw...@googlegroups.com
Hi, we are adding the oneOf and anyOf support in 3.0, and I believe the “nullable” support is being added as well.



--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Popowich

unread,
Dec 2, 2016, 12:13:48 PM12/2/16
to Swagger


On Friday, December 2, 2016 at 12:05:24 PM UTC-5, tony tam wrote:
Hi, we are adding the oneOf and anyOf support in 3.0, and I believe the “nullable” support is being added as well.

In other words, there's no way in 2.0 to specify a property of an object may be either NULL or a $ref?


tony tam

unread,
Dec 2, 2016, 12:15:00 PM12/2/16
to swagger-sw...@googlegroups.com
I think you’re describing that a value can be “null” or an “object”, forgetting specification semantics.  That would be done with the “required” flag unless I’m misunderstanding something thing.

Daniel Popowich

unread,
Dec 2, 2016, 12:32:04 PM12/2/16
to swagger-sw...@googlegroups.com

tony tam writes:
> I think you re describing that a value can be null or an object , forgetting
> specification semantics. That would be done with the required flag unless I m
> misunderstanding something thing.

I'm talking about response objects, a returned object may be:

{
"id": 12345,
"name": "John Doe",
"foo": {...a complex object...}
}

Think RDB where column foo is a reference to another table and
nullable. I always want to return "foo" to the clients, so its either
null or the complex object (the serialization of the row from the
other table).

And because a Foo is a complex object I want to create a definition of
it and reference it, keeping my spec DRY:

{
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"foo": {
"$ref": "#/definitions/Foo"
}
},
type: "object"
}

With oneOf, I can make foo:

"foo": {
oneOf: [
{"$ref": "#/definitions/Foo"},
{"type": "null"}
]
}

How can I do this now?

Toe Dipper

unread,
Jun 23, 2017, 2:54:04 PM6/23/17
to Swagger
I am also curious for support for "nullable" fields.  Support for "oneOf" and "anyOf" is not quite the same thing.  In JSON Schema, the "type" field can be an array, e.g. to indicate a nullable field:

// In JSON Schema
{
   
"type": ["string", "null"]
}
// Or, using anyOf
{
   
"anyOf":[
     
{"type": "string"},
     
{"type": "null"}
   
]
}

The end result is equivalent to "anyOf"... arguably the JSON Schema spec should simplify so that the type is always a string and "anyOf" is the only way to support this "string OR null" value.

To clear, the "required" keyword does not cover this specific use case where a property needs to exist, but its value might need to be null.

Relatedly, the "swagger-cli" npm package DOES consider the type: ["string", "null"] as valid, but the online editor does not.  Which is correct?

Ron Ratovsky

unread,
Jun 23, 2017, 2:58:44 PM6/23/17
to swagger-sw...@googlegroups.com

“null” is not a valid type in swagger, nor is “anyOf” or multi-types.


Please NOTE: This electronic message, including any attachments, may include privileged, confidential and/or inside information owned by Leaf Group. Any distribution or use of this communication by anyone other than the intended recipient(s) is strictly prohibited and may be unlawful. If you are not the intended recipient, please notify the sender by replying to this message and then delete it from your system. Thank you.

Reply all
Reply to author
Forward
0 new messages