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

Confusion around null values in spec and schema #297

Closed
csuich2 opened this issue Mar 4, 2015 · 14 comments
Closed

Confusion around null values in spec and schema #297

csuich2 opened this issue Mar 4, 2015 · 14 comments

Comments

@csuich2
Copy link

csuich2 commented Mar 4, 2015

See this topic for context: swagger-api/swagger-js#268

When I discovered those exceptions in swagger-js, I initially thought it was just an issue of error handling in the JS. During that discussion, I was informed that the Swagger spec and schema technically do not support null values for some fields (get, post, put, operations, for example).

I just wanted to provide a little feedback as a new user that this was both a bit surprising and confusing. I tried reading through both the spec and schema to determine whether my APIs behavior of serializing null values contradicted the Swagger spec or schema but couldn't find anything that indicated either way.

All that said, I'm hoping someone can explain the decision to explicitly disallow null values for these types of fields. Serializing null values from APIs seems like something that isn't uncommon and not supporting that was just a bit surprising.

Thanks!
Chris

@webron
Copy link
Member

webron commented Mar 4, 2015

Thanks for taking the time to open the issue.

JSON Schema requires you to mention that a property can be nullified. For example, take this schema:

{
  "type": "object",
  "properties": {
    "test": {
      "type": "string"
    }
  }
}

A fairly simple one, of an object with one property type that has a string value.
Using https://json-schema-validator.herokuapp.com, try validating the following two samples:

{
  "test": "hello"
}
{
  "test": null
}

You'll see that the one with the null value just doesn't validate. Since we never explicitly mention that in the JSON Schema, it means null is not a valid value anywhere. That said, the JSON Schema reflects on the spec itself, but the spec is the single source of truth. It's true that the spec does not mention it, as I had never thought it's required. Keep in mind that null is a meaningful value - when you omit a property, it means its value is not relevant for that object. However, when you nullify a property, you basically "erase" an existing value. This doesn't make much sense for the spec, plus it adds a lot of clutter and makes a spec much more difficult to read.

@maxfelker
Copy link

+1

1 similar comment
@samwalshnz
Copy link

+1

@webron
Copy link
Member

webron commented Jul 21, 2016

Tackling PR: #741

@webron
Copy link
Member

webron commented Feb 22, 2017

#894 added null support in 3.0.

@webron webron closed this as completed Feb 22, 2017
@marcelstoer
Copy link

This confused the heck out of me. I found some hints on this issue list scattered across various issues but I still opted to ask a simple question at https://stackoverflow.com/q/45575493/131929.

@darrelmiller
Copy link
Member

@marcelstoer Answered on Stackoverflow.

@webron Unless I'm reading the JSON Schema spec wrong, it looks like that validator is invalid. null is always a valid string http://json-schema.org/latest/json-schema-validation.html#rfc.section.3.1

@webron
Copy link
Member

webron commented Aug 8, 2017

which validator?

@darrelmiller
Copy link
Member

The one you referenced here #297 (comment) to justify why null is not valid.

@webron
Copy link
Member

webron commented Aug 8, 2017

Okay, and just so we're on the same page - did you reference draft 06's validation where that was for OAS2 that used draft 04?

@darrelmiller
Copy link
Member

@darrelmiller
Copy link
Member

This seems to come down to whether you consider the JSON concept of null as mapping to a string with the \x00 character.

@webron
Copy link
Member

webron commented Aug 8, 2017

Are the nul character and null as a value the same thing?

I'll give you a hint why I think that wasn't the intent. Do you see the fge part in the draft 04 URL? That's the same guy who wrote the online validator for the test in #297 (comment).

@darrelmiller
Copy link
Member

:-) LOL ok. That might be definitive then. I'll fix my answer on SO.

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

6 participants