zircote / swagger-php

A php swagger annotation and parsing library
http://zircote.github.io/swagger-php/
Apache License 2.0
5.08k stars 934 forks source link

Model properties coming out as numeric array #81

Closed leehicks closed 11 years ago

leehicks commented 11 years ago

I have been using swagger-php along with swagger-ui for a few months now, but always had the models "hard-coded" as json files because of no pure class representation internally. I recently started working with model generation, but the properties seem to not match the spec. They may be fine in the php array in the swagger obj, but the json output has them as a numeric array of json objects. See below output, generated from the tests/SwaggerTests/Fixtures/Models/Pet.php file. This seems to happen with dev-master and 0.6.*@dev.

    "Pet": {
        "id": "Pet",
        "description": null,
        "extends": null,
        "properties": [
            {
                "name": "tags",
                "type": "Array",
                "description": null,
                "allowableValues": null,
                "items": {
                    "type": "Tag"
                }
            },
            {
                "name": "id",
                "type": "long",
                "description": null,
                "allowableValues": null,
                "items": null
            },
            {
                "name": "category",
                "type": "Category",
                "description": null,
                "allowableValues": null,
                "items": null
            },
            {
                "name": "status",
                "type": "string",
                "description": "pet status in the store",
                "allowableValues": {
                    "valueType": "LIST",
                    "values": [
                        "available",
                        "pending",
                        "sold"
                    ],
                    "min": null,
                    "max": null
                },
                "items": null
            },
            {
                "name": "name",
                "type": "string",
                "description": null,
                "allowableValues": null,
                "items": null
            },
            {
                "name": "photoUrls",
                "type": "Array",
                "description": null,
                "allowableValues": null,
                "items": {
                    "type": "string"
                }
            }
        ]
    }
bfanger commented 11 years ago

Internally an array is used to store the properties, but this array is transformed in the Model::jsonSerialize() which is called automatically when you use (the preferred) $swagger->getResource() For more more control you can use the Swagger::export() or $swagger->jsonEncode()

PS: swagger-php 0.6 no longer requires pure class representation for models, they can be declared anywhere, like so:

/**
 * @SWG\Model(id="Pet",
 *     @SWG\Property(name="status",type="int"),
 *     @SWG\Property(name="statusName",type="string"),
 *     @SWG\Property(name="tags",type="Array", items="$ref:Tag")
 * )
*/
leehicks commented 11 years ago

I see what I was doing wrong. I was pulling the models directly out of the registry (to add some more dynamic pieces) and then json encoding at a later point, not using export(). I wasn't using the getResource, but I will once I get my model declarations correct.

Also, the model declaration you mentioned is exactly what I was looking for. Maybe you could update the swagger-php documentation to reflect that when time permits.

0.6@dev is much improved!

Thanks, Lee Hicks

www.dreamfactory.com

On Thu, May 9, 2013 at 6:16 PM, Bob Fanger notifications@github.com wrote:

Internally an array is used to store the properties, but this array is transformed in the Model::jsonSerialize() which is called automatically when you use (the preferred) $swagger->getResource() For more more control you can use the Swagger::export() or $swagger->jsonEncode()

PS: swagger-php 0.6 no longer requires pure class representation for models, they can be declared anywhere, like so:

/* * @SWG\Model(id="Pet", * @SWG\Property(name="status",type="int"), * @SWG\Property(name="statusName",type="string"), * @SWG\Property(name="tags",type="Array", items="$ref:Tag") * )/

— Reply to this email directly or view it on GitHubhttps://github.com/zircote/swagger-php/issues/81#issuecomment-17693412 .