-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Model definitions, array of primitive types (i.e. type : [ "string", "null" ] ) #459
Comments
That is what the |
I think that is a slightly different meaning.
An object like |
Hi, what your saying makes sense, but isn't supported in swagger. Attributes are either required or not, and each attribute needs to have a fixed dataType. In your case, it is true that it is a valid JSON Schema representation, however with swagger you'd want to solve it like such: {
"Contact" : {
"id": "Contact",
"required": [
"lastName"
],
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
} the missing / null work would need to happen before it goes in your db. |
I'm inheriting a DB schema, so some of the data we have is already null. When it comes back from the DB, it's not that a property (required or not) is missing, its that the value is null, like It still is a convenience thing for me to have a single Model that I use for swagger and JSON validation (rather than two different ones). My You're putting together some great stuff here, by the way, this has been an excellent scheme to follow for our REST API definitions! |
Weird... I would've assumed that if a property is required, it must be present and must not be null or undefined. That certainly seems a more useful definition of "required" than the one mentioned by @chimmelb above. |
You could always send your JSON through a filter that removes properties with null values... We do this in Java via Jackson's: |
@rage-shadowman - I'm not sure I understand why you don't see the usefulness for a required property to be set to An example of why this could be useful: if you are loading a JSON object into a python dictionary (the standard conversion for the
...or did I misunderstood the point you were trying to make? |
I just think that a missing property is essentially the same as a null value. Even in javascript if you use You have found a reason why it makes a difference (essentially the same is not exactly the same). You could probably work around this by using a template that you extend with the json object if you wanted to. I don't know how that syntax would look in python though. |
Well, there you have it above. Having a property set to However this is an artificial constraint that should not be imposed onto an API specification. The most obvious user case for which assuming a non-existent property is equal to A silly example: you might have a class As I said, the example above it a little silly, but especially when you have no control over the source of the data (as in the OP case, where (s)he uses a legacy DB), duck typing is a very useful technique. The definitive reason not to go with the assumption you suggested is however not the user caes, but the fact the two are simply not just the same thing in terms of formal logic (and thus implementation in many languages). |
Using my phone, unfortunately, so I'll keep it brief. Mac is absolutely right regarding the null issue - a null field is not Whether it's good to rely on it or not is besides the point.
|
Support for null value is also import to support array items type. You need to be able to define the type of an array that may contain null values. |
This is a work around for [swagger-core issue swagger-api#459](swagger-api/swagger-core#459)
This issue has drifted some since opened. I'm opening a new one here: |
… anyOf construct, so just remove the offending attributes from the model definition (swagger-api/swagger-core#459)
I'm using swagger on a project and validating the JSON HTTP requests and responses with the same swagger models. Quite handy since both are JSON v4 compliant . . . almost.
It is valid JSON for the "type" of a property to be an array of primitives. See section 5.5.2 of the JSON schema validation document.
My use case has a model object definition like this:
I use a JSON v4 compliant validation module to ensure input and outputs to our system are valid. Like when an object is returned from our database(-layer) I validate against the same Model that is defined in swagger. Sweet! However, some database fields can be null.
I use
"type" : "any"
to make swagger and my validation happy, except it's not true that any type will work, and I'm stuck with un-enforceable comments to let the user of the API know how to pass data.Supporting the JSON v4 spec with an array of types would be great when using the JSON v4 model in both swagger and validation, especially with a pass-through database that allows NULLs.
The text was updated successfully, but these errors were encountered: