You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NullPointerException in parser with additionalProperties: false #1437
I'm trying to restrict properties by setting additionalProperties: false. However, this seems to crash the YAML parser with a NullPointerException. When testing, please note that the most visible error is a syntax error from the JSON parser which kicks in after the YAML parser failed.
Main.java:
public class NpeDemo {
public static void main(String[] args) {
Swagger swagger = new SwaggerParser().read("npe-demo.yaml", null, true);
}
}
The problem happens in PropertyDeserializer.deserialize(JsonParser jp, DeserializationContext ctxt): The propertyFromNode(node) call returns null, which causes the NPE in the next line.
While it shouldn't throw NPE, please note that setting additionalProperties: false is not needed as unlike JSON schema, that's the assumption in Swagger. In a way, that's not even supported in the description and the schema validation may soon impose this restriction.
Hi @webron, I came across this comment while researching a related issue:
While it shouldn't throw NPE, please note that setting additionalProperties: false is not needed as unlike JSON schema, that's the assumption in Swagger. In a way, that's not even supported in the description and the schema validation may soon impose this restriction.
That's not consistent with the specification, which says additionalProperties "...is the same as the one from JSON Schema, only where the original definition references the JSON Schema definition, the Schema Object definition is used instead."
According to that specification, we would expect additionalProperties to have a default value of true, with the option to override it with a false value, or with a schema. Please let me know if I am missing anything here.
@tedepstein - you're right that it's inconsistent with what the spec says, but the actual error is in the spec as that was the implied intent which never made it to the docs (unfortunately). There may even still be an open ticket to change that in the doc in 2.0...
@webron OK. It looks like this is all getting revisited in OpenAPI 3.0 anyway.
For now, I'll assume the following.
additionalProperties has a default value of false
if specified, additionalProperties must have a value that is a valid OpenAPI Schema Object.
additionalProperties cannot have a value of true.
additionalProperties can be combined with regular, statically defined properties.
Note that there are some contexts in swagger-models that don't support this: ObjectProperty doesn't have additionalProperties, and MapProperty doesn't have regular properties. So in the current implementation, you can only use both regular properties and additionalProperties in a ModelImpl.
We will assume that this is incomplete implementation of the spec in Swagger-Models. We'll work around it where possible, and look to the OpenAPI 3.0 release for a more complete implementation.
Activity
webron commentedon Oct 19, 2015
While it shouldn't throw NPE, please note that setting
additionalProperties: falseis not needed as unlike JSON schema, that's the assumption in Swagger. In a way, that's not even supported in the description and the schema validation may soon impose this restriction.fehguy commentedon Oct 19, 2015
Parser is a separate project, and this is addressed in the latest parser snapshot, which will be released soon after swagger-core-1.5.4 is out.
tedepstein commentedon Jun 13, 2016
Hi @webron, I came across this comment while researching a related issue:
That's not consistent with the specification, which says additionalProperties "...is the same as the one from JSON Schema, only where the original definition references the JSON Schema definition, the Schema Object definition is used instead."
According to that specification, we would expect
additionalPropertiesto have a default value oftrue, with the option to override it with afalsevalue, or with a schema. Please let me know if I am missing anything here.webron commentedon Jun 17, 2016
@tedepstein - you're right that it's inconsistent with what the spec says, but the actual error is in the spec as that was the implied intent which never made it to the docs (unfortunately). There may even still be an open ticket to change that in the doc in 2.0...
tedepstein commentedon Jun 17, 2016
@webron OK. It looks like this is all getting revisited in OpenAPI 3.0 anyway.
For now, I'll assume the following.
additionalPropertieshas a default value offalseadditionalPropertiesmust have a value that is a valid OpenAPI Schema Object.additionalPropertiescannot have a value oftrue.additionalPropertiescan be combined with regular, statically definedproperties.swagger-modelsthat don't support this: ObjectProperty doesn't haveadditionalProperties, and MapProperty doesn't have regularproperties. So in the current implementation, you can only use both regular properties and additionalProperties in a ModelImpl.Please let me know if any of this is not correct.