0

EDIT FOR CLARITY: We're using JsonSchema2POJO in our gradle.config file to generate java classes on build. These classes are continuously deleted and created when we clean and build, so adjusting the classes rather than the gradle file or the schema isn't maintainable. In the JsonSchema2POJO gradle script we've set includeAdditionalProperties = true, and in all schemas we've set "additionalProperties": true. However, when additional properties are included in an object to be validated against the schema, Jackson throws an UnrecognizedPropertyException. The exception is not thrown if we explicitly declare an "additionalProperties": {} object in the json, but I'm looking for a way to get any additional properties to simply be added to the additionalProperties object in the java class without explicitly declaring the "additionalProperties": {} object in the json message, preferably from the gradle or schema files.

I'm sending a json request to a newly created service. The way I've been instructed to code it, several different schemas are rolled into one: all schemas have to have "someString", and depending on the value of "someString" the request gets parsed into one of several objects - one may require "someOtherString" while another may not use "someOtherString" and instead use "someInteger". Once the request comes in and the value of "someString" is parsed, it can be translated into another object based on that value.

The schema for the service looks like this:

"mySchema.json"

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "type": "object",
   "properties": {
      "someString": {
         "type": "string",
         "minLength": 1
      },
      "required": [
         "someString"
      ],
      "javaName": "mySchemaObject",
      "additionalProperties": true
   }
}

However, when I try so send the below request, I catch the exception further below:

JSON Request:

{
   "someString": "someStringValue",
   "someObject": {
      "someOtherString": someOtherStringValue
   }
}

Exception:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "someObject" (Class: com.my.package.someClass)

The same thing occurs with the below request:

{
   "someString": "someStringValue",
   "someOtherString": someOtherStringValue
}

Exception:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "someOtherString" (Class: com.my.package.someClass)

If I adjust the request to the following, it gets through the filter:

{
   "someString": "someStringValue,
   "additionalProperties": {
      "someOtherString": "someOtherStringValue"
   }
}

In all the examples I see of schemas involving additionalProperties, you don't actually need to include an "additionalProperties" object... is this unique to jackson? Is there a way to set any properties not listed in the schema as additionalProperties by default?

Wickerbough
  • 365
  • 1
  • 4
  • 15
  • (1) the `org.codehaus` packages aren’t really up-to-date. Maybe update to a current version of (`com.fasterxml`) Jackson. (2) at least in newer Jackson versions, whether or not additional properties are allowed is a global option that may need to be enabled explicitly. – Carsten May 26 '20 at 14:54
  • I'll see what I can do - this is a work project and they're pretty particular about what they permit. While the mentality is regrettable, I imagine they'll tell me that if I can do it without changing the libraries we're currently using I should. – Wickerbough May 26 '20 at 15:12
  • Then it might be worth exploring the `ObjectMapper` settings for your specific library version. It might be disabled by default and could be as simple as a one-line configuration. – Carsten May 26 '20 at 15:15
  • In your schema, "someString" is at the top level of the schema, which won't be recognized. It needs to be underneath `"properties": { ... }` Similarly, `"javaName": "mySchemaObject"` doesn't do anything in the schema either (and "mySchemaObject" also has no meaning - is this intended to be a `$ref`?) – Ether May 26 '20 at 16:02
  • https://stackoverflow.com/questions/39156293/jackson-deserialize-extra-fields-as-map – Matthew Warman May 26 '20 at 16:47
  • @Ether Whoops! I'm manually transcribing form another PC; I've adjusted the schema code in the post to match what's going on in the code. myObject in this case is a dynamic field; it can't go in the schema, but it shouldn't stop the schema from parsing. Ideally, it should be mapped to an 'additionalProperties' map in the java class. – Wickerbough May 26 '20 at 17:15
  • @MatthewWarman hmmm, I've just gone through and updated all the places where we call the ObjectMapper and added the line: ``` mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); ``` I've also gone into the java classes generated by jsonschema2pojo and ensured that they have a @JsonAnysetter and @JsonAnygetter and sure enough the additionalProperties field has a getter and setter with those annotations... Which is driving me nuts. – Wickerbough May 26 '20 at 17:16
  • Despite that it's not the solution I want to end up with, just for a test I went ahead and added the annotation @JsonIgnoreProperties(ignoreUnknown = true) to the class in question and I'm still getting the same error... so I'm kind of at a loss. – Wickerbough May 26 '20 at 17:24
  • Okay... I'm not tremendously familair with the tools that I'm using, but I think I see what's potentially causing the issue... all of the annotations and imports on the generated classes are from com.fasterxml.jackson, but the error that's being thrown is from org.codehaus.jackson... so I think whoever initially wrote the code used both versions, so I'm correcting the issue in the newer version but the older one is still finding an issue... which means it's being used somewhere during the deserialization, since the issue is occuring when trying to read in the data. Blugh. Fingers crossed.. – Wickerbough May 26 '20 at 17:37
  • Also, `"additionalProperties": true` is underneath `properties` when it should be a sibling. As it is now you are declaring that there could be a property named "additionalProperties", and its value can be anything (its schema is true). – Ether May 28 '20 at 15:36

0 Answers0