diff --git a/packages/bruno-converters/src/openapi/openapi-to-bruno.js b/packages/bruno-converters/src/openapi/openapi-to-bruno.js index 8569d5c8a..1b5908226 100644 --- a/packages/bruno-converters/src/openapi/openapi-to-bruno.js +++ b/packages/bruno-converters/src/openapi/openapi-to-bruno.js @@ -489,7 +489,7 @@ const transformOpenapiRequestItem = (request, usedNames = new Set()) => { if (CONTENT_TYPE_PATTERNS.JSON.test(normalizedMimeType)) { brunoRequestItem.request.body.mode = 'json'; - if (bodySchema && bodySchema.type === 'object') { + if (bodySchema && (bodySchema.type === 'object' || bodySchema.properties)) { let _jsonBody = buildEmptyJsonBody(bodySchema); brunoRequestItem.request.body.json = JSON.stringify(_jsonBody, null, 2); } @@ -498,7 +498,7 @@ const transformOpenapiRequestItem = (request, usedNames = new Set()) => { } } else if (normalizedMimeType === 'application/x-www-form-urlencoded') { brunoRequestItem.request.body.mode = 'formUrlEncoded'; - if (bodySchema && bodySchema.type === 'object') { + if (bodySchema && (bodySchema.type === 'object' || bodySchema.properties)) { each(bodySchema.properties || {}, (prop, name) => { brunoRequestItem.request.body.formUrlEncoded.push({ uid: uuid(), @@ -511,7 +511,7 @@ const transformOpenapiRequestItem = (request, usedNames = new Set()) => { } } else if (normalizedMimeType === 'multipart/form-data') { brunoRequestItem.request.body.mode = 'multipartForm'; - if (bodySchema && bodySchema.type === 'object') { + if (bodySchema && (bodySchema.type === 'object' || bodySchema.properties)) { each(bodySchema.properties || {}, (prop, name) => { brunoRequestItem.request.body.multipartForm.push({ uid: uuid(), diff --git a/packages/bruno-converters/tests/openapi/openapi-to-bruno/openapi-body.spec.js b/packages/bruno-converters/tests/openapi/openapi-to-bruno/openapi-body.spec.js new file mode 100644 index 000000000..c9360d733 --- /dev/null +++ b/packages/bruno-converters/tests/openapi/openapi-to-bruno/openapi-body.spec.js @@ -0,0 +1,67 @@ +import { describe, it, expect } from '@jest/globals'; +import openApiToBruno from '../../../src/openapi/openapi-to-bruno'; + +describe('openapi requestBody with $ref', () => { + it('should import body fields when requestBody uses $ref to components/requestBodies with inline schema (no explicit type: object)', () => { + const openApiSpec = ` +openapi: "3.0.0" +info: + version: "1.0.0" + title: "RequestBody Ref Inline Schema Test" +servers: + - url: "https://api.example.com" +paths: + /salesInvoices: + post: + summary: "Creates a salesInvoice" + operationId: "postSalesInvoice" + requestBody: + $ref: '#/components/requestBodies/salesInvoice' + responses: + '201': + description: "A new salesInvoice has been successfully created" +components: + requestBodies: + salesInvoice: + required: true + content: + application/json: + schema: + properties: + id: + type: string + format: uuid + number: + type: string + maxLength: 20 + externalDocumentNumber: + type: string + maxLength: 35 + invoiceDate: + type: string + format: date-time + dueDate: + type: string + format: date-time +`; + + const result = openApiToBruno(openApiSpec); + + // Should have one request item + expect(result.items.length).toBe(1); + const request = result.items[0]; + + // Body mode should be json + expect(request.request.body.mode).toBe('json'); + + // Body should contain the properties from the schema + expect(request.request.body.json).not.toBeNull(); + + const bodyJson = JSON.parse(request.request.body.json); + expect(bodyJson).toHaveProperty('id'); + expect(bodyJson).toHaveProperty('number'); + expect(bodyJson).toHaveProperty('externalDocumentNumber'); + expect(bodyJson).toHaveProperty('invoiceDate'); + expect(bodyJson).toHaveProperty('dueDate'); + }); +});