简体   繁体   English

使用 Swagger/OpenAPI 规范指定要作为 GET 参数发送的数组

[英]Specifying an array to be sent as a GET parameter using Swagger/OpenAPI spec

The following swagger definition:以下招摇定义:

swagger: '2.0'
host: example.com
paths:
  /theResource:
    get:
      parameters:
        - $ref: '#/parameters/fields'
parameters:
  fields:
    name: fields
    description: Fields to return
    in: query
    type: array
    collectionFormat: multi
    uniqueItems: true
    items:
      type: string
      enum:
        - column1
        - column2
        - column3

Will generate the following URL, and the server will only receive fields equal to column3 .将生成以下 URL,服务器将只接收等于column3 fields

http://example.com/theResource?fields=column1&fields=column2&fields=column3

How do I change it so that it sends an array in the parameter?如何更改它以便它在参数中发送一个数组? For instance:例如:

http://example.com/theResource?fields[]=column1&fields[]=column2&fields[]=column3

In PHP you can use http_build_query , that will construct proper query part of URL for you.在 PHP 中,您可以使用http_build_query ,它将为您构造 URL 的正确查询部分。

<?php

$query = http_build_query([
    'fields' => [
        'one', 'two', 'three'
    ],
]);

$url = "https://example.com/?".$query;

And yes, the fields[] syntax should do the trick.是的, fields[]语法应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM