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

extending definitions #254

Closed
DerManoMann opened this issue Oct 12, 2015 · 4 comments
Closed

extending definitions #254

DerManoMann opened this issue Oct 12, 2015 · 4 comments

Comments

@DerManoMann
Copy link
Collaborator

Not sure if this is the right place or the swagger specs, but I'd like to extend a definition.

Basically I have 2 versions of a model:
a) without ids or timestamps (that is what is used to persist new instances)
b) a full version with timestamps/id coming back from the persistence layer

I figured I can leave the id/timestamps as optional, but that doesn't look really nice in the UI and is not quite the truth.

And finally, thanks for the good work :)

@bfanger
Copy link
Collaborator

bfanger commented Oct 18, 2015

You could try the allOf:

{
  ...
  "definitions": {
   "Thing": {
     ...
  },
  "ThingWithTimestamp": {
    "description": "Extended thing",
    "allOf": [
      {
        "$ref": "#/definitions/Thing"
      }, {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int32",
            "description": "The ID",
          }
        },
        "required": [
          "id"
        ]
      }
    ]
  }
}

More on the subject can be found at: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#composition-and-inheritance-polymorphism

@DerManoMann
Copy link
Collaborator Author

Cool, thanks. In fact I did notice the allOf late last week and figured that might be worth looking at!

@DerManoMann
Copy link
Collaborator Author

Ok, tried it and it is almost good :)

The data looks good and swagger-ui does do the right thing. However, with the current master I get this exception:

Call Stack:
    ...
    1.4707   21654864   7. Swagger\scan() .../utils/SwaggerBuildCommand.php:60
    9.9388   30249152   8. Swagger\Analysis->process() .../vendor/zircote/swagger-php/src/functions.php:44
    9.9871   30490592   9. Swagger\Processors\InheritProperties->__invoke() .../vendor/zircote/swagger-php/src/Analysis.php:275

Test definitions:

 * @SWG\Swagger(
 *   @SWG\Definition(
 *     definition="base",
 *     required={"active"},
 *     @SWG\Property(property="active", description="active", type="boolean")
 *   ),
 *   @SWG\Definition(
 *     definition="sub",
 *     allOf={
 *       @SWG\Schema(ref="#/definitions/base"),
 *       @SWG\Schema(required={"foo"}, @SWG\Property(property="foo", description="foo", type="string"))
 *     }
 *   )
 * )

Generated definitions:

        "base": {
            "required": [
                "active"
            ],
            "properties": {
                "active": {
                    "description": "active",
                    "type": "boolean"
                }
            }
        },
        "sub": {
            "allOf": [
                {
                    "$ref": "#\/definitions\/base"
                },
                {
                    "required": [
                        "foo"
                    ],
                    "properties": {
                        "foo": {
                            "description": "foo",
                            "type": "string"
                        }
                    }
                }
            ]
        }

@DerManoMann DerManoMann reopened this Oct 19, 2015
@bfanger
Copy link
Collaborator

bfanger commented Oct 23, 2015

Should be fixed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants