3

I have an API which is accepting query param as Object. I am using this to add multiple filters to filter the result.

When I hit the request from swagger, I am getting null for my filter object in the controller.

userFilter is the POJO class. It is used as a query param and in the controller, it is coming as null.

On swagger, it is showing as below

swagger screenshot

userFilter object is not getting constructed and getting NullPointerException in controller class when trying to access any field from userFilter.

2
  • Please post the definition of the userFilter parameter from your OpenAPI YAML/JSON file, and also your controller code. Commented Jul 13, 2021 at 16:53
  • userFilter is the POJO class, and in the controller, it is coming as null. I have used userFilter as query param. Commented Jul 15, 2021 at 4:58

1 Answer 1

6

I got the solution from swagger.io.

As per the explanation, content is used in complex serialization scenarios that are not covered by style and explode. For example, if we need to send a JSON string in the query string like so:

filter={"type":"t-shirt","color":"blue"}

In this case, we need to wrap the parameter schema into content/<media-type> as shown below.

We need to add content = {@Content(schema = @Schema(type = "object"))} to the @Parameter.

@Parameter(description = "Filters", required = true, content = {@Content(schema = @Schema(type = "object"))})

In JSON format it will look like below.

parameters:
  - in: query
    name: filter
    
    # Wrap 'schema' into 'content.<media-type>'
    content:
      application/json:  # <---- media type indicates how to serialize / deserialize the parameter content
        schema:
          type: object
          properties:
            type:
              type: string
            color:
              type: string
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.