From 18871b955d00658ef305c633c0454a6978ffda1c Mon Sep 17 00:00:00 2001 From: Melsy Huamani Date: Fri, 26 Jun 2026 16:37:44 -0500 Subject: [PATCH 1/2] fix: schema validation for 204 endpoints --- CHANGELOG.md | 7 ++ package-lock.json | 4 +- package.json | 2 +- seeds/generatorTestBody204Initial.json | 43 +++++++++++ seeds/generatorTestBody204Result.json | 43 +++++++++++ src/generator/environmentVariablesNames.js | 1 + src/generator/testBody.js | 2 +- test/generator-envVariables.js | 84 ++++++++++++++++++++++ test/generator-testBody.js | 9 +++ 9 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 seeds/generatorTestBody204Initial.json create mode 100644 seeds/generatorTestBody204Result.json create mode 100644 test/generator-envVariables.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ace168..050baf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [2.4.1-beta-1] - 2026-06-26 + +### Fixed +- Fix schema validation crash on 204 No Content endpoints when `validate_schema: true`. + + ## [2.4.0] - 2026-05-07 ### Added diff --git a/package-lock.json b/package-lock.json index 4cd3e68..e8dffa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "openapi2postman", - "version": "2.4.0", + "version": "2.4.1-beta-1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "openapi2postman", - "version": "2.4.0", + "version": "2.4.1-beta-1", "license": "ISC", "dependencies": { "js-yaml": "^4.1.0", diff --git a/package.json b/package.json index 1ccfcf3..0b05d30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi2postman", - "version": "2.4.0", + "version": "2.4.1-beta-1", "description": "openapi2postman", "bin": { "o2p": "index.js" diff --git a/seeds/generatorTestBody204Initial.json b/seeds/generatorTestBody204Initial.json new file mode 100644 index 0000000..84cb643 --- /dev/null +++ b/seeds/generatorTestBody204Initial.json @@ -0,0 +1,43 @@ +{ + "name": "/pets/{id}-204", + "aux": { + "status": "204", + "body": false, + "consumes": false, + "bodyResponse": { + "500": { + "type": "object", + "required": ["code", "message"], + "properties": { + "code": { "type": "integer", "format": "int32" }, + "message": { "type": "string" } + } + } + } + }, + "response": [], + "request": { + "method": "DELETE", + "header": [], + "body": { "mode": "raw", "raw": "" }, + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets/{{id}}", + "host": ["{{host}}{{port}}{{basePath}}"], + "path": ["/pets/{{id}}"] + } + }, + "event": [ + { + "listen": "test", + "script": { + "id": "test-204-uuid", + "type": "text/javascript", + "exec": [ + "pm.test(\"Status code is 204\", function () {", + " pm.response.to.have.status(204);", + "});" + ] + } + } + ] +} diff --git a/seeds/generatorTestBody204Result.json b/seeds/generatorTestBody204Result.json new file mode 100644 index 0000000..84cb643 --- /dev/null +++ b/seeds/generatorTestBody204Result.json @@ -0,0 +1,43 @@ +{ + "name": "/pets/{id}-204", + "aux": { + "status": "204", + "body": false, + "consumes": false, + "bodyResponse": { + "500": { + "type": "object", + "required": ["code", "message"], + "properties": { + "code": { "type": "integer", "format": "int32" }, + "message": { "type": "string" } + } + } + } + }, + "response": [], + "request": { + "method": "DELETE", + "header": [], + "body": { "mode": "raw", "raw": "" }, + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets/{{id}}", + "host": ["{{host}}{{port}}{{basePath}}"], + "path": ["/pets/{{id}}"] + } + }, + "event": [ + { + "listen": "test", + "script": { + "id": "test-204-uuid", + "type": "text/javascript", + "exec": [ + "pm.test(\"Status code is 204\", function () {", + " pm.response.to.have.status(204);", + "});" + ] + } + } + ] +} diff --git a/src/generator/environmentVariablesNames.js b/src/generator/environmentVariablesNames.js index 4fdb0ae..07c0dc1 100644 --- a/src/generator/environmentVariablesNames.js +++ b/src/generator/environmentVariablesNames.js @@ -140,6 +140,7 @@ module.exports = function() { } function extractVariablesFromTest(aux, execCode, numerateItem, items){ if (global.configurationFile.schema_is_inline === false) { + if (!aux.bodyResponse || !aux.bodyResponse[aux.status]) return; const key = numerateItem + 'schemaTest'; for (let i in execCode){ if (execCode[i] === 'var schema = pm.environment.get("schemaTest");'){ diff --git a/src/generator/testBody.js b/src/generator/testBody.js index 28544a4..23983e4 100644 --- a/src/generator/testBody.js +++ b/src/generator/testBody.js @@ -9,7 +9,7 @@ module.exports = function() { return function get(postmanRequest, configurationFile) { - if ((!postmanRequest.aux.bodyResponse || !postmanRequest.aux.bodyResponse[postmanRequest.aux.status]) && postmanRequest.aux.status === 200) { + if (!postmanRequest.aux.bodyResponse || !postmanRequest.aux.bodyResponse[postmanRequest.aux.status]) { console.warn( '\x1b[33m%s\x1b[0m', `Test: ${postmanRequest.request.method} ${postmanRequest.name} (${postmanRequest.aux.suffix === undefined ? 'main test' : postmanRequest.aux.suffix.trim()}) without schema validation test because it has a different response than 'application/json'` diff --git a/test/generator-envVariables.js b/test/generator-envVariables.js new file mode 100644 index 0000000..162597f --- /dev/null +++ b/test/generator-envVariables.js @@ -0,0 +1,84 @@ +/** Part of APIAddicts. See LICENSE fileor full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/ + +const assert = require('assert'); + +describe('generator-envVariables', () => { + + let savedConfig; + let savedEnvVars; + + beforeEach(() => { + savedConfig = global.configurationFile; + savedEnvVars = global.environmentVariables; + global.environmentVariables = {}; + }); + + afterEach(() => { + global.configurationFile = savedConfig; + global.environmentVariables = savedEnvVars; + }); + + it('204 no body schema is inline false', () => { + global.configurationFile = { schema_is_inline: false, schema_pretty_print: false, is_inline: false }; + + const collection = [ + { + name: 'pets', + item: [ + { + name: 'pets', + item: [ + { + name: '/pets/{id}-204', + aux: { + numerateItem: '1', + status: '204', + bodyResponse: { + '500': { + type: 'object', + required: ['code', 'message'], + properties: { + code: { type: 'integer', format: 'int32' }, + message: { type: 'string' } + } + } + }, + queryParams: [] + }, + request: { + method: 'DELETE', + header: [], + body: { mode: 'raw', raw: '' }, + url: { + raw: '{{host}}{{port}}{{basePath}}/pets/1', + host: ['{{host}}{{port}}{{basePath}}'], + path: ['/pets/1'] + } + }, + event: [ + { + listen: 'test', + script: { + id: 'test-204-uuid', + type: 'text/javascript', + exec: [ + 'pm.test("Status code is 204", function () {', + ' pm.response.to.have.status(204);', + '});' + ] + } + } + ] + } + ] + } + ] + } + ]; + + const items = require('../src/generator/environmentVariablesNames.js')(collection); + const schemaTestItem = items.find(item => item.key && item.key.includes('schemaTest')); + assert.strictEqual(schemaTestItem, undefined, 'No schemaTest env variable should be created for 204 No Content'); + }); + +}); diff --git a/test/generator-testBody.js b/test/generator-testBody.js index a6a0246..5c6d6ee 100644 --- a/test/generator-testBody.js +++ b/test/generator-testBody.js @@ -13,4 +13,13 @@ describe('generator-testBody', () => { assert.deepStrictEqual(definition, definitionResult); }); + it('204 no body', () => { + + const definitionInitial = require('../seeds/generatorTestBody204Initial.json'); + const definitionResult = require('../seeds/generatorTestBody204Result.json'); + + const definition = require('../src/generator/testBody.js')(definitionInitial, {schema_pretty_print: false}); + assert.deepStrictEqual(definition, definitionResult); + }); + }); \ No newline at end of file From 149a66e68c6bc129480397b109e93f37e6d8d386 Mon Sep 17 00:00:00 2001 From: Melsy Huamani Date: Fri, 26 Jun 2026 16:47:16 -0500 Subject: [PATCH 2/2] fix: sonar issues --- src/generator/environmentVariablesNames.js | 2 +- src/generator/testBody.js | 11 +++++++---- test/generator-envVariables.js | 16 ++++++++-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/generator/environmentVariablesNames.js b/src/generator/environmentVariablesNames.js index 07c0dc1..c4337c0 100644 --- a/src/generator/environmentVariablesNames.js +++ b/src/generator/environmentVariablesNames.js @@ -140,7 +140,7 @@ module.exports = function() { } function extractVariablesFromTest(aux, execCode, numerateItem, items){ if (global.configurationFile.schema_is_inline === false) { - if (!aux.bodyResponse || !aux.bodyResponse[aux.status]) return; + if (!aux.bodyResponse?.[aux.status]) return; const key = numerateItem + 'schemaTest'; for (let i in execCode){ if (execCode[i] === 'var schema = pm.environment.get("schemaTest");'){ diff --git a/src/generator/testBody.js b/src/generator/testBody.js index 23983e4..26133b0 100644 --- a/src/generator/testBody.js +++ b/src/generator/testBody.js @@ -10,12 +10,15 @@ module.exports = function() { return function get(postmanRequest, configurationFile) { if (!postmanRequest.aux.bodyResponse || !postmanRequest.aux.bodyResponse[postmanRequest.aux.status]) { - console.warn( - '\x1b[33m%s\x1b[0m', - `Test: ${postmanRequest.request.method} ${postmanRequest.name} (${postmanRequest.aux.suffix === undefined ? 'main test' : postmanRequest.aux.suffix.trim()}) without schema validation test because it has a different response than 'application/json'` + const noBodyStatuses = ['204', '205', '304']; + if (!noBodyStatuses.includes(String(postmanRequest.aux.status))) { + console.warn( + '\x1b[33m%s\x1b[0m', + `Test: ${postmanRequest.request.method} ${postmanRequest.name} (${postmanRequest.aux.suffix === undefined ? 'main test' : postmanRequest.aux.suffix.trim()}) without schema validation test because it has a different response than 'application/json'` ); + } return postmanRequest; - } + } let schemaJSON; if (configurationFile.schema_is_inline === false) { diff --git a/test/generator-envVariables.js b/test/generator-envVariables.js index 162597f..c6674e3 100644 --- a/test/generator-envVariables.js +++ b/test/generator-envVariables.js @@ -1,6 +1,6 @@ /** Part of APIAddicts. See LICENSE fileor full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/ -const assert = require('assert'); +const assert = require('node:assert'); describe('generator-envVariables', () => { @@ -8,18 +8,18 @@ describe('generator-envVariables', () => { let savedEnvVars; beforeEach(() => { - savedConfig = global.configurationFile; - savedEnvVars = global.environmentVariables; - global.environmentVariables = {}; + savedConfig = globalThis.configurationFile; + savedEnvVars = globalThis.environmentVariables; + globalThis.environmentVariables = {}; }); afterEach(() => { - global.configurationFile = savedConfig; - global.environmentVariables = savedEnvVars; + globalThis.configurationFile = savedConfig; + globalThis.environmentVariables = savedEnvVars; }); it('204 no body schema is inline false', () => { - global.configurationFile = { schema_is_inline: false, schema_pretty_print: false, is_inline: false }; + globalThis.configurationFile = { schema_is_inline: false, schema_pretty_print: false, is_inline: false }; const collection = [ { @@ -77,7 +77,7 @@ describe('generator-envVariables', () => { ]; const items = require('../src/generator/environmentVariablesNames.js')(collection); - const schemaTestItem = items.find(item => item.key && item.key.includes('schemaTest')); + const schemaTestItem = items.find(item => item.key?.includes('schemaTest')); assert.strictEqual(schemaTestItem, undefined, 'No schemaTest env variable should be created for 204 No Content'); });