diff --git a/src/core/plugins/spec/selectors.js b/src/core/plugins/spec/selectors.js index f8580156333..35842823ba1 100644 --- a/src/core/plugins/spec/selectors.js +++ b/src/core/plugins/spec/selectors.js @@ -546,7 +546,11 @@ export const getOAS3RequiredRequestBodyContentType = (state, pathMethod) => { if (requestBody.getIn(["required"])) { requiredObj.requestBody = requestBody.getIn(["required"]) } - requestBody.getIn(["content"]).entrySeq().forEach((contentType) => { // e.g application/json + const requestBodyContent = requestBody.getIn(["content"]) + if (!requestBodyContent) { + return requiredObj + } + requestBodyContent.entrySeq().forEach((contentType) => { // e.g application/json const key = contentType[0] if (contentType[1].getIn(["schema", "required"])) { const val = contentType[1].getIn(["schema", "required"]).toJS() diff --git a/test/unit/core/plugins/spec/selectors.js b/test/unit/core/plugins/spec/selectors.js index 53df1c71d8a..672c87baca0 100644 --- a/test/unit/core/plugins/spec/selectors.js +++ b/test/unit/core/plugins/spec/selectors.js @@ -15,6 +15,7 @@ import { consumesOptionsFor, taggedOperations, isMediaTypeSchemaPropertiesEqual, + getOAS3RequiredRequestBodyContentType, validationErrors } from "core/plugins/spec/selectors" @@ -1239,6 +1240,83 @@ describe("taggedOperations", function () { expect(result.toJS()).toEqual([]) }) }) +describe("getOAS3RequiredRequestBodyContentType", () => { + const pathMethod = ["/test", "post"] + + it("should return default requiredObj when requestBody is missing", () => { + const state = fromJS({ + resolvedSubtrees: { + paths: { + "/test": { + post: {} + } + } + } + }) + + const result = getOAS3RequiredRequestBodyContentType(state, pathMethod) + + expect(result).toEqual({ + requestBody: false, + requestContentType: {} + }) + }) + + it("should not crash and return requiredObj when requestBody has no content", () => { + const state = fromJS({ + resolvedSubtrees: { + paths: { + "/test": { + post: { + requestBody: { + required: true + } + } + } + } + } + }) + + const result = getOAS3RequiredRequestBodyContentType(state, pathMethod) + + expect(result).toEqual({ + requestBody: true, + requestContentType: {} + }) + }) + + it("should collect required schema fields per content type when content is present", () => { + const state = fromJS({ + resolvedSubtrees: { + paths: { + "/test": { + post: { + requestBody: { + required: true, + content: { + "application/json": { + schema: { + required: ["name", "email"] + } + } + } + } + } + } + } + } + }) + + const result = getOAS3RequiredRequestBodyContentType(state, pathMethod) + + expect(result).toEqual({ + requestBody: true, + requestContentType: { + "application/json": ["name", "email"] + } + }) + }) +}) describe("isMediaTypeSchemaPropertiesEqual", () => { const stateSingleMediaType = fromJS({ resolvedSubtrees: {