postmanlabs / openapi-to-postman

Plugin for converting OpenAPI 3.0 specs to the Postman Collection (v2) format
Apache License 2.0
922 stars 199 forks source link

Add support for JSON objects as query parameters #785

Open gorbster opened 8 months ago

gorbster commented 8 months ago

The OpenAPI 3 spec allows for query parameters that are of type object. However when this is converted to a Postman collection, the parameters are converted to key / value string pairs. The below example is from an OpenAPI spec that defines a request with a GET query param of type object (location) that must include oneOf the listed properties (zip or address). When converted to a Postman collection, zip is created as a top-level parameter instead of a nested parameter within the location object.

{
     "schema":
     {
         "type": "object",
         "properties":
         {
             "zip":
             {
                 "type": "string",
                 "description": "Your zip code",
                 "maxLength": 5,
                 "minLength": 5
             },
             "address":
             {
                 "type": "string",
                 "description": "Your address"
             }
         },
         "oneOf":
         [
             {
                 "required":
                 [
                     "zip"
                 ]
             },
             {
                 "required":
                 [
                     "address"
                 ]
             }
         ],
         "maxProperties": 1,
         "minProperties": 1
     },
     "in": "query",
     "name": "location",
     "required": true
 }
gorbster commented 8 months ago

This was originally raised in the Postman community forum here.

VShingala commented 7 months ago

Hey @gorbster, OpenAPI specification suggests that when the object is used for query parameters, by default the value of it's properties should be at top level.

Ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#style-examples

For detailed explanation, for query, parameters called style and explode which determines how value of query will be handled are defaulted to form and true. And for these values, you can see from table that top level param name is not maintained and individual params will be formed for each key value pairs.

To keep top level param intact, you can mention explode as false in you definition. i.e. below will provide you result with location intact.

{
     "schema":
     {
         "type": "object",
         "properties":
         {
             "zip":
             {
                 "type": "string",
                 "description": "Your zip code",
                 "maxLength": 5,
                 "minLength": 5
             },
             "address":
             {
                 "type": "string",
                 "description": "Your address"
             }
         }
     },
     "in": "query",
     "name": "location",
     "required": true,
     "explode": false
 }