swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Wrong Query parameter serialization when using Object

Open SebastienRvl opened this issue 7 years ago • 7 comments

Description

We are using objects in query parameters for a get operations, when we call the service, object parameters are not serialized as expected. The toString method is call and le url is corrupted

/v1/declarations/etablissements?page=class%20PageInType%20%7B%0A%20%20%20%20page%3A%2010%0A%20%20%20%20size%3A%2050%0A%7D&filtre=class%20FiltreRechercherEtablissementDSNType%20%7B%0A%20%20%20%20siret%3A%20null%0A%20%20%20%20siren%3A%20808332670%0A%20%20%20%20annee%3A%202017%0A%20%20%20%20mois%3A%2001%0A%20%20%20%20localite%3A%20null%0A%20%20%20%20effectifCalculeDebutDePeriode%3A%20null%0A%20%20%20%20effectifCalculeFinDePeriode%3A%20null%0A%20%20%20%20denomination%3A%20null%0A%20%20%20%20codeNAF%3A%20null%0A%7D&tri=class%20TriRechercherEtablissementsDSNType%20%7B%0A%20%20%20%20siret%3A%20null%0A%20%20%20%20siren%3A%20desc%0A%20%20%20%20mois%3A%20null%0A%20%20%20%20localite%3A%20null%0A%20%20%20%20effectifCalculeFinDePeriode%3A%20null%0A%20%20%20%20effectifCalculeDebutDePeriode%3A%20null%0A%20%20%20%20denomination%3A%20null%0A%20%20%20%20codeNAF%3A%20null%0A%20%20%20%20annee%3A%20null%0A%7D

Swagger-codegen version

3.0.0-SNAPSHOT

Swagger declaration file content or url

tags: - declarations parameters: - name: page in: query required: false style: form schema: $ref: '#/components/schemas/PageIn_Type' - name: filtre in: query required: false style: form schema: $ref: '#/components/schemas/FiltreRechercherEtablissementDSN_Type' - name: tri in: query required: false style: form explode: false schema: $ref: '#/components/schemas/TriRechercherEtablissementsDSN_Type' responses: ...

PageIn_Type:
  type: object
  properties:
    page:
      type: number
    size:
      type: number

... FiltreRechercherEtablissementDSN_Type: type: object properties: siret: type: string siren: type: string annee: type: string mois: type: string localite: type: string effectifCalculeDebutDePeriode: type: string effectifCalculeFinDePeriode: type: string denomination: type: string codeNAF: type: string

Command line used for generation
 		<plugin>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-codegen-maven-plugin</artifactId>
			<version>${swagger-codegen-version}</version>
			<executions>
				<execution>
					<id>generate-client</id>
					<goals>
						<goal>generate</goal>
					</goals>
					<phase>generate-sources</phase>
					<configuration>
						<inputSpec>${project.basedir}/src/main/resources/dsnci_ws_consultation_be_v1.0.yaml</inputSpec>
						<language>java</language>
						<apiPackage>fr.recouv.dsn.ws.rest.api.v1</apiPackage>
						<modelPackage>fr.recouv.dsn.ws.rest.dto.v1</modelPackage>
						<output>${generated-swagger-path}</output>
						<withXml>true</withXml>
						<generateApis>true</generateApis>
						<generateApiTests>true</generateApiTests>
					</configuration>
				</execution>
			</executions>
		</plugin>

SebastienRvl avatar Aug 22 '18 11:08 SebastienRvl

@SebastienRvl did you get an answer ? how did you solve this issue ?

petitout avatar May 22 '20 15:05 petitout

I also tested the php and python clients, they have the same issue. Looking trough the generator templates i barely see any references to the explode option

wiebren avatar Feb 09 '21 12:02 wiebren

I think you want to specify your object parameters in the query string using content, not just schema (see "schema vs content" at https://swagger.io/docs/specification/describing-parameters/). For example, I have this:

- name: page_id
  in: query
  required: true
  content:
    application/json:
      schema:
        $ref: "#/components/schemas/PageId"

Unfortunately, the generated Python client code doesn't seem to take it into account.

sknick avatar May 18 '21 14:05 sknick

Are there any updates on this issue? @SebastienRvl

altarrok avatar Feb 22 '22 21:02 altarrok

Still facing this issue

{
  "openapi": "3.0.1",
  "info": {
    "title": "API",
    "version": "v1"
  },
  "paths": {
    "/api/Search/{index}": {
        "get": {
          "tags": [
            "Search"
          ],
          "summary": "Get items matched by given pattern.",
          "parameters": [
            {
              "name": "index",
              "in": "path",
              "description": "Index.",
              "required": true,
              "schema": {
                "type": "string"
              }
            },
            {
              "name": "pattern",
              "in": "query",
              "description": "Pattern.",
              "schema": {
                "type": "object",
                "additionalProperties": {
                  "type": "string"
                }
              }
            },
            {
              "name": "limit",
              "in": "query",
              "description": "Limit.",
              "schema": {
                "type": "integer",
                "format": "int32",
                "default": 100
              }
            },
            {
              "name": "dateField",
              "in": "query",
              "description": "Name of Date field in Pattern.",
              "schema": {
                "type": "string"
              }
            }
          ],
          "responses": {
            "404": {
              "description": "Not Found"
            },
            "400": {
              "description": "Bad Request"
            },
            "401": {
              "description": "Unauthorized"
            },
            "200": {
              "description": "Success",
              "content": {
                "text/plain": {
                  "schema": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/SearchResult"
                    }
                  }
                },
                "application/json": {
                  "schema": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/SearchResult"
                    }
                  }
                },
                "text/json": {
                  "schema": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/SearchResult"
                    }
                  }
                }
              }
            }
          }
        }
    }
  },
  "components": {
    "schemas": {
      "SearchResult": {
        "type": "object",
        "properties": {
          "Total": {
            "type": "integer",
            "format": "int64"
          },
          "Results": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            },
            "nullable": true
          }
        },
        "additionalProperties": false
      }
    }
  }
}

sample pattern request with swagger ui:

{
  "additionalProp1": "string",
  "additionalProp2": "string",
  "additionalProp3": "string"
}

results with correct curl / request url:

curl -X 'GET' \
  'https://raw.githubusercontent.com/api/Search/test?additionalProp1=string&additionalProp2=string&additionalProp3=string&limit=100' \
  -H 'accept: text/plain'
https://raw.githubusercontent.com/api/Search/test?additionalProp1=string&additionalProp2=string&additionalProp3=string&limit=100

but generated javascript client adds all items in pattern object under pattern query string

cc @wing328 @mefioo

kbegiedza avatar Dec 07 '22 10:12 kbegiedza

Is this issue ever gonna be fixed? Facing the same problem

DenuxPlays avatar Mar 26 '24 20:03 DenuxPlays