Forum Discussion

ravik51378's avatar
ravik51378
New Contributor
2 months ago

enum query parameter with "explode: false" not working

I have a query parameter whose data type is enum as below. When I send the parameter with one value as "?status=ACTIVE" it is working fine. But when I send with two values as "?status=ACTIVE,FAIL" it throws 404-NotFound. Since it has "explode: false", we should be able to send multiple values separated by comma. Can you please help with this issue?

The expected behavior is, when I send "?status=ACTIVE,FAIL", then I should receive it as ArrayList of 2 elements: ["ACTIVE", "FAIL"]. But I am receiving the whole comma separated string as one element as ["ACTIVE,FAIL"]

parameters:
    status:
        in: query
        style: form
        explode: false
        schema:
            type: array
            items:
                $ref: '#/components/schemas/status'
                    
schemas:
    status:
      type: string
      enum:
        - ACTIVE
        - FAIL
        - COMPLETE
        - INPROGRESS
        - IGNORE

6 Replies

  • Humashankar's avatar
    Humashankar
    Champion Level 0

    Hi ravik51378 

    Could you please try the following:

     

    1. Remove the "style: form" attribute from the parameter definition.
    2. Change the "schema" definition to use a plain array type instead of "$ref":

     

    schema:

    type: array

    items:

    type: string

    enum:

    - ACTIVE

    - FAIL

    - COMPLETE

    - INPROGRESS

    - IGNORE

     

    This should allow you to send multiple values for the "status" parameter, separated by commas, without exploding the array.

     

    Please let me know if this resolves the issue

    • ravik51378's avatar
      ravik51378
      New Contributor

      Humashankar  - I tried this, but it is not working. When I send It is sending the whole comma separated string as one element of the ArrayList (same as original behavior)

  • Humashankar's avatar
    Humashankar
    Champion Level 0

    Hi ravik51378

    Looks like the "explode: false" parameter is not having the desired effect, and the entire comma-separated string is still being treated as a single element in the ArrayList.

    Can you please provide more details about the query and the exact behavior you're seeing? Additionally, what version you are using?

    • ravik51378's avatar
      ravik51378
      New Contributor

      Hi Humashankar 

      I am using Swagger openapi: 3.0.0 version and specified "jaxrs-jersey" as language for code generation as below in swagger-codegen-maven-plugin entry:

      :

                  <executions>
                    <execution>
                      <id>java-api-v1</id>
                      <goals>
                        <goal>generate</goal>
                      </goals>
                      <configuration>
                        <inputSpec>${swagger-gen-api-v1-file}</inputSpec>
                        <language>jaxrs-jersey</language>
                        <addCompileSourceRoot>false</addCompileSourceRoot>
                        <configOptions>
                          <sourceFolder>src/gen/java</sourceFolder>
                          <serializableModel>true</serializableModel>
                        </configOptions>
                      </configuration>
                    </execution>
                  </executions>

      Actually, now I am trying with String (instead of enum) to make it simple. So I changed parameter definition as below:

      parameters:
          status:
              in: query
              style: form
              explode: false
              schema:
                  type: array
                  items:
                      type: string

      So, in the generated code I see the query parameter as below:

      @Parameter(in = ParameterIn.QUERY, description = "") @QueryParam("status") List<String> status

      With this when I send the query param like "?status=abc,xyz,test", I am receiving it as:

      satus = ["abc,xyz,test"]

      But I am expecting it as:

      satus = ["abc","xyz","test"]

      • Humashankar's avatar
        Humashankar
        Champion Level 0

        Thanks for the inputs ravik51378 

        Looks Swagger code generator treats as a single string value instead of an array of individual strings

        Can you try doing this by adding a custom validation method to the query parameter, like below,

        @Parameter(in = ParameterIn.QUERY, description = "")
        @QueryParam("status")
        List<String> status;

        @Validation
        public void validateStatus(List<String> status) {
        String[] statusArray = status.get(0).split(",");
        this.status = Arrays.asList(statusArray);
        }

        Out of it, this will split the incoming string into an array of individual strings using the "," delimiter, and then set the "status" field to a list of those strings.

        Regards