diff --git a/src/core/utils/index.js b/src/core/utils/index.js index f3290dd468d..2e0ac60c944 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -612,6 +612,17 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec parameterContentMediaType } = getParameterSchema(param, { isOAS3 }) + // Compose anyOf/oneOf schemas: if the schema lacks a direct type but has + // anyOf or oneOf, derive the type from the first sub-schema so that + // type-specific validation applies correctly. + if (paramDetails && !paramDetails.get("type")) { + const firstCompositeType = + paramDetails.getIn(["anyOf", 0, "type"]) || paramDetails.getIn(["oneOf", 0, "type"]) + if (firstCompositeType) { + paramDetails = paramDetails.set("type", firstCompositeType) + } + } + return validateValueBySchema(value, paramDetails, paramRequired, bypassRequiredCheck, parameterContentMediaType, isOAS3) } diff --git a/test/unit/core/utils.js b/test/unit/core/utils.js index f6b7d9d01c5..e44402ab832 100644 --- a/test/unit/core/utils.js +++ b/test/unit/core/utils.js @@ -1061,6 +1061,75 @@ describe("utils", () => { value = undefined assertValidateParam(param, value, ["Required field is not provided"]) }) + + it("validates required OAS3 parameters with anyOf schema", () => { + // missing value with anyOf should report required error + param = { + required: true, + schema: { + anyOf: [ + { type: "integer" }, + { type: "string" } + ] + } + } + value = undefined + assertValidateOas3Param(param, value, ["Required field is not provided"]) + + // valid integer value with anyOf + param = { + required: true, + schema: { + anyOf: [ + { type: "integer" }, + { type: "string" } + ] + } + } + value = 123 + assertValidateOas3Param(param, value, []) + + // valid string value with anyOf (matches second sub-schema type via first) + param = { + required: true, + schema: { + anyOf: [ + { type: "string" }, + { type: "integer" } + ] + } + } + value = "hello" + assertValidateOas3Param(param, value, []) + }) + + it("validates required OAS3 parameters with oneOf schema", () => { + // missing value with oneOf should report required error + param = { + required: true, + schema: { + oneOf: [ + { type: "integer" }, + { type: "string" } + ] + } + } + value = undefined + assertValidateOas3Param(param, value, ["Required field is not provided"]) + + // valid integer value with oneOf + param = { + required: true, + schema: { + oneOf: [ + { type: "integer" }, + { type: "string" } + ] + } + } + value = 123 + assertValidateOas3Param(param, value, []) + }) }) describe("fromJSOrdered", () => {