Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require array to contain at least one element in Swagger Schema Object definition

I'm having a Schema Object definition like this in my swagger.yaml:

User:
  type: object
  properties:
    username:
      type: string
      description: the user name
    colors:
      type: array
      items: {
        type: string,
        enum: [ "red", "blue", "green" ]
      }
      description: user must have one or more colors associated
  required:
    - username
    - colors

However, the generated server still happily accepts POST requests using this schema object as required body parameter that do not contain any colors field.

Can I configure Swagger in a way that the color field is always required in a User schema object and ideally also must contain at least one or more items from the enum?

like image 825
Byte Commander Avatar asked Oct 28 '16 08:10

Byte Commander


People also ask

How does swagger define byte array?

Using the definition above the swagger code generator generates an object that accepts byte[] array as the body field new Job(). setBody(new byte[1]) . After converting the API definition to OpenAPI the definition for that object stayed the same but the openapi code generator now requires org. springframework.

How do you declare an empty array in swagger?

You can specify an empty array [] as an example for your array schema. This will override the default examples values generated by Swagger UI. Show activity on this post. shows your users that photoUrls is an array of strings.

What is schema in swagger?

OpenAPI 3.0 data types are based on an extended subset JSON Schema Specification Wright Draft 00 (aka Draft 5). The data types are described using a Schema object. To learn how to model various data types, see the following topics: Data Types.


1 Answers

Use minItems: 1. Additionally you can enforce uniqueItems within the array.

    colors:
      type: array
      minItems: 1
      uniqueItems: true
      items:
        type: string
        enum: [ "red", "blue", "green" ]
like image 137
Helen Avatar answered Sep 18 '22 12:09

Helen