This commit is contained in:
pooja-bruno
2025-12-04 12:44:29 +05:30
parent a31d19c7dc
commit 3219e8af8e
2 changed files with 130 additions and 3 deletions

View File

@@ -128,15 +128,15 @@ const buildEmptyJsonBody = (bodySchema, visited = new Map()) => {
let _jsonBody = {};
each(bodySchema.properties || {}, (prop, name) => {
if (prop.type === 'object') {
if (prop.type === 'object' || prop.properties) {
_jsonBody[name] = buildEmptyJsonBody(prop, visited);
} else if (prop.type === 'array') {
if (prop.items && prop.items.type === 'object') {
if (prop.items && (prop.items.type === 'object' || prop.items.properties)) {
_jsonBody[name] = [buildEmptyJsonBody(prop.items, visited)];
} else {
_jsonBody[name] = [];
}
} else if (prop.type === 'integer') {
} else if (prop.type === 'integer' || prop.type === 'number') {
_jsonBody[name] = 0;
} else if (prop.type === 'boolean') {
_jsonBody[name] = false;

View File

@@ -155,4 +155,131 @@ components:
expect(fieldNames).toContain('file');
expect(fieldNames).toContain('description');
});
it('should handle number and integer types with correct default values', () => {
const openApiSpec = `
openapi: "3.0.0"
info:
version: "1.0.0"
title: "Number Type Test"
servers:
- url: "https://api.example.com"
paths:
/orders:
post:
summary: "Create order"
operationId: "createOrder"
requestBody:
content:
application/json:
schema:
properties:
quantity:
type: integer
price:
type: number
discount:
type: number
name:
type: string
active:
type: boolean
responses:
'201':
description: "Order created"
`;
const result = openApiToBruno(openApiSpec);
expect(result.items.length).toBe(1);
const request = result.items[0];
expect(request.request.body.mode).toBe('json');
expect(request.request.body.json).not.toBeNull();
const bodyJson = JSON.parse(request.request.body.json);
// integer type should be 0
expect(bodyJson.quantity).toBe(0);
// number type should be 0 (not empty string)
expect(bodyJson.price).toBe(0);
expect(bodyJson.discount).toBe(0);
// string type should be empty string
expect(bodyJson.name).toBe('');
// boolean type should be false
expect(bodyJson.active).toBe(false);
});
it('should handle nested arrays with objects containing number fields', () => {
const openApiSpec = `
openapi: "3.0.0"
info:
version: "1.0.0"
title: "Nested Array Number Test"
servers:
- url: "https://api.example.com"
paths:
/orders:
post:
summary: "Create orders"
operationId: "createOrders"
requestBody:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Order'
responses:
'201':
description: "Orders created"
components:
schemas:
Order:
properties:
id:
type: string
quantity:
type: integer
items:
type: array
items:
$ref: '#/components/schemas/LineItem'
LineItem:
properties:
productId:
type: string
price:
type: number
`;
const result = openApiToBruno(openApiSpec);
expect(result.items.length).toBe(1);
const request = result.items[0];
expect(request.request.body.mode).toBe('json');
expect(request.request.body.json).not.toBeNull();
const bodyJson = JSON.parse(request.request.body.json);
// Should be an array with one order object
expect(Array.isArray(bodyJson)).toBe(true);
expect(bodyJson.length).toBe(1);
const order = bodyJson[0];
expect(order.id).toBe('');
expect(order.quantity).toBe(0);
// Nested items array should have one LineItem
expect(Array.isArray(order.items)).toBe(true);
expect(order.items.length).toBe(1);
const lineItem = order.items[0];
expect(lineItem.productId).toBe('');
expect(lineItem.price).toBe(0); // number type should be 0, not empty string
});
});