Swagger-php: Return array of objects in Schema, ref alternative

Created on 16 Sep 2015  路  10Comments  路  Source: zircote/swagger-php

Hello, is it possible to describe array of returning objects with @SWG\Property's instead of ref="" usage ? I mean the alternative way of ref="#/definitions/Pet"

     *         @SWG\Schema(
     *             type="array",
     *             @SWG\Items(ref="#/definitions/Pet")
     *         ),

Most helpful comment

Currently @SWG\Item is implemented as a "Items Object" is "a limited subset of JSON-Schema's items object." according to the spec

| Field | Type | Description |
| --- | --- | --- |
| type | string | Required. The internal type of the array. The value MUST be one of "string", "number", "integer", "boolean", or "array". Files and models are not allowed. |

However, using a $ref to an type="object" also shouldn't be allowed. which made me realize that the "Items Object" is only referred to from a "Parameter Object" or "Header Object"

In a future release of swagger-php you'll be able to write:

     *         @SWG\Schema(
     *             type="array",
     *             @SWG\Items(
     *                 type="object",
     *                 @SWG\Property(property="id", type="integer"),
     *                 @SWG\Property(property="name", type="string")
     *             )
     *         ),

All 10 comments

Currently @SWG\Item is implemented as a "Items Object" is "a limited subset of JSON-Schema's items object." according to the spec

| Field | Type | Description |
| --- | --- | --- |
| type | string | Required. The internal type of the array. The value MUST be one of "string", "number", "integer", "boolean", or "array". Files and models are not allowed. |

However, using a $ref to an type="object" also shouldn't be allowed. which made me realize that the "Items Object" is only referred to from a "Parameter Object" or "Header Object"

In a future release of swagger-php you'll be able to write:

     *         @SWG\Schema(
     *             type="array",
     *             @SWG\Items(
     *                 type="object",
     *                 @SWG\Property(property="id", type="integer"),
     *                 @SWG\Property(property="name", type="string")
     *             )
     *         ),

:tractor:

still type="object" isn't a allowed Type in @SWG\Items
error =>
@SWG\Items()->type="object" not allowed inside a @SWG\Parameter() must be "string", "number", "integer", "boolean", "array"

Version: 2.0.6

Hello, when i use this feature, the problem is no Properties generate in items.

 *      @SWG\Parameter(
 *          name="body",
 *          in="body",
 *          required=true,
 *          @SWG\Schema(
 *              required={"product_id"},
 *              @SWG\Property(property="product_id",  type="string",  ),
 *              @SWG\Property(
 *                  property="products",
 *                  type="array",
 *                  @SWG\Items(
 *                      type="object",
 *                      @SWG\Property(property="name", type="string", ),
 *                      @SWG\Property(property="category", type="string",),
 *                  ),
 *              ),
 *              @SWG\Property(
 *                  property="user_summary",
 *                  title="UserSummary",
 *                  @SWG\Property(property="username", type="string",),
 *                  @SWG\Property(property="birthday", type="string",),
 *              ),
 *          ),
 *      ),

The json schema of 'items' just have 'type' no properties:

          "properties": {
            "product_id": {
              "type": "string"
            },
            "products": {
              "type": "array",
              "items": {
                "type": "object"
              }
            }

Weird i get:

{
    "swagger": "2.0",
    "paths": {},
    "definitions": {},
    "parameters": {
        "body": {
            "name": "body",
            "in": "body",
            "required": true,
            "schema": {
                "required": [
                    "product_id"
                ],
                "properties": {
                    "product_id": {
                        "type": "string"
                    },
                    "products": {
                        "type": "array",
                        "items": {
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "category": {
                                    "type": "string"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "user_summary": {
                        "title": "UserSummary",
                        "properties": {
                            "username": {
                                "type": "string"
                            },
                            "birthday": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
}

Maybe an older version of doctrine is causing the issue? I've got doctrine/annotations at v1.2.7 running PHP 7.0.6.

doctrine/annotations

       "name": "doctrine/annotations",
        "version": "v1.2.7",
        "source": {
            "type": "git",
            "url": "https://github.com/doctrine/annotations.git",
            "reference": "f25c8aab83e0c3e976fd7d19875f198ccf2f7535"
        },

PHP version is:
PHP 7.0.3 (cli) (built: Feb 18 2016 11:11:24) ( NTS )

@chrisfoon I was using master instead of 2.0.6 which already contained a fix for the items property nesting issue. Just released 2.0.7.

Got it, Thanks!

Hi,
It seems that I have the exact same issue as @chrisfoon using the exact same config:
https://github.com/zircote/swagger-php/issues/245#issuecomment-218431714

Version: 2.0.10
doctrine/annotations v1.4.0

Project on Symfony 3.3.2, PHP 7.1.5 and using NelmioApiDocBundle in v3.0.0-BETA3

Create a testcase/test project and try to localize / fix the bug so you can create a pull-request with the fix to the misbehaving project.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

praditha picture praditha  路  3Comments

phansys picture phansys  路  5Comments

DerManoMann picture DerManoMann  路  3Comments

ricardovanlaarhoven picture ricardovanlaarhoven  路  5Comments

bfanger picture bfanger  路  4Comments