Skip to content

Possible bug with TokenFilter #572

@jhaber

Description

@jhaber

Hello, I was trying to update a library I own, jackson-jaxrs-propertyfiltering, to use a TokenFilter and ran into an issue that struck me as surprising and/or a bug. For some background, this library extends the jackson-jaxrs-json-provider and let's callers filter JSON responses using query params (so if you just need the id and name of each object you could pass ?property=id&property=name).

Previously this library would serialize the response to a JsonNode, apply the filtering to the JsonNode, and then serialize the filtered JsonNode to the HTTP response. This worked fine, but is very inefficient. I tried updating this to use a TokenFilter to apply the filtering in a streaming fashion and things worked great except we had some test failures around empty objects. For example, given a JSON document like:

[
   {
      "id":123,
      "name":"abc",
      "other":"def"
   },
   {
      "id":123,
      "name":"abc",
      "other":"def"
   },
   {
      "id":123,
      "name":"abc",
      "other":"def"
   }
]

If the request looks like ?property=id&property=name then things work great and the response looks like:

[
   {
      "id":123,
      "name":"abc"
   },
   {
      "id":123,
      "name":"abc"
   },
   {
      "id":123,
      "name":"abc"
   }
]

However, if the response filters out all the properties, such as ?property=fake, then we end up with just the empty string:

So we return an HTTP 200 with no JSON at all and the caller blows up. Before the switch to a TokenFilter, this would have filtered out all of the object keys but retained the JSON structure so you end up with something like:

[{},{},{}]

I pushed a complete example to my fork:
jhaber@3e637a8

Is there a way to achieve this sort of behavior currently? If not, would you be open to a PR that adds a new option to FilteringGeneratorDelegate that enables this behavior?

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions