Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model inside a model support in response definition #10

Closed
tecnom1k3 opened this issue Oct 19, 2016 · 5 comments
Closed

Model inside a model support in response definition #10

tecnom1k3 opened this issue Oct 19, 2016 · 5 comments
Assignees

Comments

@tecnom1k3
Copy link

I have the following model definitions:

/**
 * @rest\model address object defining station address.
 * @rest\property string address_line1 address line1
 * @rest\property string address_line2 address line2
 * @rest\property string city city
 * @rest\property string state state
 * @rest\property string postal_code postal code
 * @rest\property string country_code country code
 * @rest\property string phone phone
 * @rest\property string fax fax
 *
 * @rest\model stations Returns list of station for a given supplier id.
 * @rest\property int id supplier Id
 * @rest\property int supplier supplier Id
 * @rest\property string name  Supplier Name
 * @rest\property address Address Station Address
 * @rest\property string latitude latitude
 * @rest\property string longitude longitude
 * @rest\property string timezone timezone
 * @rest\property string station_type Station Type
 */

and later on declaring the response type as follows:

 /** @rest\endpoint /stations/{supplier_id}
     * @rest\method GET Returns list of station for a given supplier id.
     * @rest\response 200 stations
    */

so I'm trying to make a reference of a model inside another model. but when I execute, I get the following exception: Property format not recognized: 'address'

is model inside a model supported the same way as an array of models?

if the definition is changed as follows, the generation executes successfuly, but does not match the real output of my API

/**
 * @rest\model address object defining station address.
 * @rest\property string address_line1 address line1
 * @rest\property string address_line2 address line2
 * @rest\property string city city
 * @rest\property string state state
 * @rest\property string postal_code postal code
 * @rest\property string country_code country code
 * @rest\property string phone phone
 * @rest\property string fax fax
 *
 * @rest\model stations Returns list of station for a given supplier id.
 * @rest\property int id supplier Id
 * @rest\property int supplier supplier Id
 * @rest\property string name  Supplier Name
 * @rest\property array(address) Address Station Address
 * @rest\property string latitude latitude
 * @rest\property string longitude longitude
 * @rest\property string timezone timezone
 * @rest\property string station_type Station Type
 */

thank you

@vanderlee
Copy link
Owner

That's a good idea!

From the top of my head, I don't know if the Swagger/Open-API spec permits this type of referencing. If it does, than implementing it should be easy. If it does not, it could still be implemented but will take a bit more effort.

The current "support" for array references is probably a bug; this was not intentional and has to do with the way SG handles types. Could you describe what you think the result should be and what SG currently generates?

I'll try to find some time this weekend to look into this.

@tecnom1k3
Copy link
Author

I think Swagger does permit, at least at swagger editor. Consider the following yaml:

swagger: '2.0'
info:
  title: API
  description: Description
  version: "1.0"
host: myhost.com
schemes:
  - https
basePath: /
consumes:
  - application/json
produces:
  - application/json
paths:
  /stations:
    get:
      description: |
        Returns list of station for a given supplier id.
      responses:
        200:
          description: An Object defining response from Station
          schema:
              $ref: '#/definitions/Stations'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'
definitions:
  Error:
    type: object
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
      fields:
        type: string
  Address:
    description: Object defining Address.
    properties:
      address_line1:
        type: string
      address_line2:
        type: string
      city:
        type: string
      state:
        type: string
      postal_code:
        type: string
      country_code:
        type: string
      phone:
        type: string
      fax:
        type: string
  Stations:
    type: array
    items:
      $ref: '#/definitions/Station'
  Station:
    description: Detailsof station.
    properties:
      id:
        type: integer
      name:
        type: string
      address:
        $ref: '#/definitions/Address'

As you can see, the Stations schema contains and array of Station objects (that is why I was expecting that @rest\property array(address) to work fine). At the same time, Station contains a reference to the address object. This way, editor.swagger.io does not complaints and produces the following output:

image

I can get SG to generate an array of referenced objects, but fail to generate a referenced object within another referenced object.

Hope this helps, please let me know if you need additional information. I would like to contribute, but at this time I find myself with very limited time :( I'm also attaching YAML above converted to JSON

Thanks!
swagger (1).json.zip

@vanderlee
Copy link
Owner

This was a pretty simple fix, adding some useful functionality.
Thanks for the issue and all the example code, which greatly helped finding and fixing this issue.
Fix and unittest coverage included in 2.3.7
Please let me know if this works for you.

@tecnom1k3
Copy link
Author

Awesome!

Will update and test, and will let you know.

@tecnom1k3
Copy link
Author

Confirming the generated docs work as expected in the swagger editor using version 2.3.7. Thanks

@vanderlee vanderlee self-assigned this Dec 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants