Using oneOf with path parameter returns error
Issue #361
new
I’m trying to use a path parameter that is oneOf
2 strings that have different pattern - one matches A-Z, the other 0-9.
openapi: 3.0.3
info:
title: atlassian/swagger-request-validator-issue
version: 1.0.0
paths:
/foo/{bar}:
get:
parameters:
- $ref: '#/components/parameters/barPathParam'
responses:
200:
description: description
components:
parameters:
barPathParam:
name: bar
in: path
required: true
schema:
example: 'ABCD'
oneOf:
- $ref: '#/components/schemas/TypeA'
- $ref: '#/components/schemas/TypeB'
schemas:
TypeA:
type: string
minLength: 1
maxLength: 5
pattern: '^[A-Z]{1,5}$'
example: 'ABCD'
TypeB:
type: string
minLength: 1
maxLength: 5
pattern: '^[0-9]{1,5}$'
example: '1234'
Request /foo/1234
fails:
validator.validateRequest(
SimpleRequest.Builder
.get("/foo/1234")
.build()
)
with error message (but is expected to pass validation):
ERROR - Instance failed to match exactly one schema (matched 0 out of 2): [/oneOf/0: Instance type (integer) does not match any allowed primitive type (allowed: ["string"]), /oneOf/1: Instance type (integer) does not match any allowed primitive type (allowed: ["string"])]
ERROR - Instance type (integer) does not match any allowed primitive type (allowed: ["string"]): []
ERROR - Instance type (integer) does not match any allowed primitive type (allowed: ["string"]): []
Request /foo/ABCD
fails:
validator.validateRequest(
SimpleRequest.Builder
.get("/foo/ABCD")
.build()
)
with error message (but is expected to pass validation):
Unable to parse JSON - Unrecognized token 'ABCD': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"ABCD"; line: 1, column: 5].
Request /foo/1A2B
fails:
validator.validateRequest(
SimpleRequest.Builder
.get("/foo/1A2B")
.build()
)
with error message (but is expected to fail validation with failed to match exactly one schema (matched 0 out of 2)
):
Unable to parse JSON - Unexpected character ('A' (code 65)): Expected space separating root-level values
at [Source: (String)"1A2B"; line: 1, column: 3].
When using 1234
the problem seems to be that:
- SchemaValidator.java#lines-226 returns
IntNode
instead ofTextNode
, becauseschema.getType()
returnsnull
Related issue - https://bitbucket.org/atlassian/swagger-request-validator/issues/321/oneof-validation-fails-happy-path-case but in my case it’s not an object
, so I can't solve this by using additionalProperties
or required
.