usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
26.87k stars 1.23k forks source link

PUT body does not get imported from openapi yaml file #3383

Open FabianDach opened 1 week ago

FabianDach commented 1 week ago

I have checked the following:

Describe the bug

When importing a collection from an openapi yaml file, the request body will not be imported if the http method is PUT.

.bru file to reproduce the bug

YAML FILE:

---
openapi: 3.0.3
info:
  title: Example API
  version: 1.0.0
paths:
  /:
    post:
      operationId: testPost
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/test"
    put:
      operationId: testPut
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/test"

components:
  schemas:
    test:
      type: object
      properties:
        id:
          format: int64
          type: integer
        name:
          type: string

POST bru:


meta {
  name: testPost
  type: http
  seq: 1
}

post {
  url: {{baseUrl}}/test/
  body: json
  auth: none
}

body:json {
  {
    "id": "",
    "name": ""
  }
}

PUT .bru:


meta {
  name: testPut
  type: http
  seq: 2
}

put {
  url: {{baseUrl}}/test/
  body: json
  auth: none
}

Screenshots/Live demo link

image image

arshan1019 commented 1 day ago

I was able to reproduce the bug. I'll be happy to work on this issue. 👍

sreelakshmi-bruno commented 1 day ago

Hi @FabianDach, thanks for reporting this. @arshan1019, please go for it!

directentis1 commented 1 day ago

I also got problem with PUT Request's body. It's seem like that with a large request, thee payload has been stripped down some byte in the end, make the request failed.

I redirected to a proxy and find out that.

arshan1019 commented 1 day ago

Hey @directentis1 @FabianDach @sreelakshmi-bruno

I don't think the issue is specific to PUT requests. The issue is there regardless of the Request Method

Here’s my YAML file for reference:

openapi: 3.0.3
info:
  title: Example API
  version: 1.0.0
paths:
  /:
    patch:
      operationId: testPatch
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/test"
    put:
      operationId: testPut
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/test"
    post:
      operationId: testPost
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/test"
components:
  schemas:
    test:
      type: object
      properties:
        id:
          format: int64
          type: integer
        name:
          type: string

The output: for PATCHRequest: Patch Request

for PUT Request: PUT Request

for POST Request: Post Request Notice the POST Request body is also not imported.

arshan1019 commented 1 day ago

This is the code responsible for resolution of the references.

I suspect the visitedItems Set that is shared among the recursive calls. When the first reference is resolved, the visitedItems set is updated with the refPath. Now for the 2nd and 3rd (considering the linked yaml file in the above comment), the code checks if the refPath already present in the visitedItems Set. Since its already present in the Set and marked as visited it does not parse (resolve) the reference resulting in no body for the 2nd and 3rd requests.

Additionally, during the recursive resolution process, if a $ref is encountered again, the function uses the shared visitedItems

image

Pls correct me if I am wrong. I am new to this stuff.

arshan1019 commented 1 day ago

@sreelakshmi-bruno @directentis1 @FabianDach Hopefully, I was able to solve the issue. The key was to pass a new instance of visitedItems Set for each recursive call by using new Set(visitedItems). This way, each branch of the recursion maintains its own tracking of resolved references.

After the fix: Output:

https://github.com/user-attachments/assets/a533d4a5-ed07-4b5c-9aa9-7aff04e8a04e

generated .bru files 👉 testPatch.bru

meta {
  name: testPatch
  type: http
  seq: 3
}

post {
  url: {{baseUrl}}/
  body: json
  auth: none
}

body:json {
  {
    "id": "",
    "name": ""
  }
}

testPost.bru

meta {
  name: testPost
  type: http
  seq: 1
}

patch {
  url: {{baseUrl}}/
  body: json
  auth: none
}

body:json {
  {
    "id": "",
    "name": ""
  }
}

testPut.bru

meta {
  name: testPut
  type: http
  seq: 2
}

put {
  url: {{baseUrl}}/
  body: json
  auth: none
}

body:json {
  {
    "id": "",
    "name": ""
  }
}

what do you guys think?