0

I need to describe a multipart query that has an array of strings. But I ran into a problem, in the query, the array elements are combined into one string instead of being separate string items. I am using OpenApi 3.0.3

              "multipart/form-data": {
                "schema": {
                    "type": "object",
                    "required": ["image"],
                    "properties": {
                        "image": {
                            "type": "string",
                            "format": "base64",
                            "description": "Banner image `1920x90, 2mb`"
                        },
                        "name": {
                            "type": "string",
                            "example": "Docs banner",
                            "description": "Banner name"
                        },
                        "link": {
                            "type": "string",
                            "description": "Banner link"
                        },
                        "page[]": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            },
                            "example": ["HOME", "LIVE", "CHANNELS"],
                            "enum":[
                                "HOME",
                                "LIVE",
                                "CHANNELS",
                                "ARTISTS",
                                "DISCOVER",
                                "MYLIST",
                                "PROGRAM",
                                "PLAYER"
                             ],
                             "description":"Banner pages"
                        }
                    }
                }
            }

What I received: page: [ 'HOME,LIVE,CHANNELS' ] What I expect: page: [ 'HOME','LIVE','CHANNELS' ]

1
  • The issue is likely with the server implementation. What server-side framework/library do you use? Can you post your controller code with annotations? Commented May 5, 2022 at 16:00

1 Answer 1

2

It's not very clear where exactly do you receive page: [ 'HOME,LIVE,CHANNELS' ], but looks like in enum there are possible values for items of your array. Try this:

"multipart/form-data": {
    "schema": {
        "type": "object",
        "required": ["image"],
        "properties": {
            "image": {
                "type": "string",
                "format": "base64",
                "description": "Banner image `1920x90, 2mb`"
            },
            "name": {
                "type": "string",
                "example": "Docs banner",
                "description": "Banner name"
            },
            "link": {
                "type": "string",
                "description": "Banner link"
            },
            "page[]": {
                "type": "array",
                "items": {
                    "type": "string",
                    "enum":[
                        "HOME",
                        "LIVE",
                        "CHANNELS",
                        "ARTISTS",
                        "DISCOVER",
                        "MYLIST",
                        "PROGRAM",
                        "PLAYER"
                     ],
                     "example": "HOME"
                },
                "example": [ "HOME", "LIVE", "CHANNELS" ],
                "description":"Banner pages"
            }
        }
    }
}

You can look for more details at the specification for adding examples.

This structure allows you to send multiple string enum items for page element, screenshot from swagger editor is below:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I am getting this value in the request that swagger sends. The solution you suggested works, but it only allows you to select one element, while I need the ability to add multiple elements to the array
@Luzz: yyunikov's array definition is in fact correct (even though it probably isn't related to the issue in question). The enum must be alongside type: items rather than alongside type: array. To select multiple array items in Swagger UI, you can Ctrl+click and Shift-click the items in the rendered list box.
@Luzz can you show an example? what does swagger sends mean exactly? you can check the spec on editor.swagger.io. the description of such API allows sending multiple items. I've attached the picture from Swagger editor.
@Luzz updated the answer, let me know if it's clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.