postmanlabs / openapi-to-postman

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

Request with multiple application/x-www-form-urlencoded request body and response examples don't get imported properly #791

Open duykhanh412 opened 6 months ago

duykhanh412 commented 6 months ago

This PR https://github.com/postmanlabs/openapi-to-postman/pull/768 has added the support for multiple examples to be generated as request examples. It works well when the request body type is json and others, however, it doesn't work well when the request body type is application/x-www-form-urlencoded.

Below is the OpenAPI yaml:

openapi: 3.0.1
info:
  title: URLEncoded Request With Multiple Request Body Examples
  version: v1
paths:
  /Foo:
    post:
      summary: Foo
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                foo:
                  type: string
                bar:
                  type: string
            examples:
              Example 1:
                value:
                  foo: abc
                  bar: def
              Example 2:
                value:
                  foo: xyz
      responses:
        "200":
          description: Successful
          content:
            application/json:
              examples:
                Example 1:
                  value:
                    first_name: abc
                    last_name: def
                Example 2:
                  value:
                    first_name: xyz

When importing to Postman:

You can see that the request bodies for both examples are exactly the same and values are some random text that get generated automatically.

I've put in a few debugging points and can see that for json request body, the examples section is passed into the schemaUtils.resolveBodyData function and then mapped to the examples in the response, whereas, for urlencoded request body, the requestBody examples section is ignored entirely and only the schema is passed in. The schemaUtils.resolveBodyData tries to then read from the example section within the schema instead, which only supports one example. So even if I've tried to add multiple examples into the example section of the schema instead, it would still not work, Postman would instead try to merge all the request body examples together into one request body to be used by both examples in that case.

Ideally, when multiple examples are provided in the urlencoded request body of a request, it should be generated as response examples just like json request body type.

VShingala commented 4 months ago

@duykhanh412 Thanks for raising the issue! Yeah, since JSON bodies are the most used schemas, we started with the support for it. We'll work on the same for both URL encoded and FormData in future. I'll update here once we have some update on this.