kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

Validating an enum array does not seem to work

Open jamesraby opened this issue 5 years ago • 3 comments

I have the following schema:

    items:
      type: string
      enum:
      - Admin
      - Prescriber
      - Clinician

When I try to validate the array field with input ["Admin"] - I get the following error:

code=400, message=Request body has an error: doesn't match the schema: Error at \"/permissions\":JSON value is not one of the allowed values, internal=Request body has an error: doesn't match the schema: Error at \"/permissions\":JSON value is not one of the allowed values\nSchema:\n {\n \"enum\": [\n \"Prescriber\",\n \"Admin\",\n \"Clinician\"\n ],\n \"items\": {\n \"type\": \"string\"\n },\n \"type\": \"array\"\n }\n\nValue:\n [\n \"Admin\"\n ]\n"

It seems like it is comparing the array ["Admin"] vs iterating through the array on line: https://github.com/getkin/kin-openapi/blob/989d00f2a3e831bd8fa14a4c2abd17a37d684f50/openapi3/schema.go#L641-L656

jamesraby avatar Apr 20 '20 17:04 jamesraby

Indeed this VisitJSON implementation seems wrong. It's using value to check the schema instead of the other way around.

It's probably time to replace it with https://github.com/xeipuuv/gojsonschema It would have to transform the OpenAPI schema to JSON Schema first (see: https://github.com/mikunn/openapi-schema-to-json-schema/blob/master/lib/converters/schema.js ) This transformation would not be needed for specs >= 3.1 though as per https://github.com/OAI/OpenAPI-Specification/pull/1977

If you want to take this on I'll gladly help!

fenollp avatar Apr 21 '20 10:04 fenollp

Would not mind contributing - but this may be too big a fish to swallow! Let me investigate

jamesraby avatar Apr 24 '20 17:04 jamesraby

I've cleaned up error message, and it seems like you have enum on the array level, not item.

Schema: {"enum": ["Prescriber", "Admin", "Clinician" ], "items": { "type": "string" }, "type": "array" }
Value: [ "Admin" ]

iron-s avatar Jul 22 '20 12:07 iron-s

Indeed the cited YAML and the error message don't match. Thanks @iron-s !

Looks like schema should instead be either of:

type: array
items:
  type: string
  enum:
  - Admin
  - Prescriber
  - Clinician

or

type: array
items:
  type: string
enum:
- [Admin]
- [Prescriber]
- [Clinician]

Note, that last schema will cause a panic due to https://github.com/getkin/kin-openapi/issues/646


The issue you're having is because you schema is

type: array
items:
  type: string
enum:
- Admin
- Prescriber
- Clinician

(note how the enum values do not match the surrounding schema)

fenollp avatar Oct 24 '22 13:10 fenollp

Closing this as this was identified as not being this lib's issue.

fenollp avatar Oct 24 '22 13:10 fenollp