fix: openapi body import

This commit is contained in:
pooja-bruno
2025-12-03 17:12:25 +05:30
parent a3d2d35d2e
commit 5680a8b729
2 changed files with 70 additions and 3 deletions

View File

@@ -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(),

View File

@@ -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');
});
});