Skip to content

ClassCastException: io.swagger.models.RefModel cannot be cast to io.swagger.models.ModelImpl #936

Closed

Description

@McFoggy

Hi,

I am trying to generate static documentation for my API definition

java -XX:MaxPermSize=256M -Xmx1024M -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i swagger.json -l html -o target

using both master & develop_2.0 branch I have the following exception

java.lang.ClassCastException: io.swagger.models.RefModel cannot be cast to io.swagger.models.ModelImpl
        at io.swagger.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:532)
        at io.swagger.codegen.DefaultGenerator.processModels(DefaultGenerator.java:452)
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:125)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:124)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)

I have isolated my issue. It comes from the definition of a model using allOf and 2 references to other models.

Here is a simplified json swagger api definition to reproduce:

{
    "swagger": "2.0",
    "info": {
        "title": "My demo API",
        "description": "My demo API documentation",
        "version": "1.0.0"
    },
    "host": "myserver",
    "schemes": [
        "http"
    ],
    "basePath": "/resources/v1",
    "paths": {
        "/clients/{clientId}": {
            "get": {
                "summary": "get info on a specific client",
                "tags": [
                    "Clients"
                ],
                "parameters": [
                    {
                        "in": "path",
                        "name": "clientId",
                        "type": "integer",
                        "required": true,
                        "format": "int64"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "retrieve data of the corresponding client",
                        "schema": {
                            "$ref": "#/definitions/Client"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "Client": {
            "allOf": [
                {
                    "$ref": "#/definitions/Person"
                },
                {
                    "$ref": "#/definitions/Address"
                }
            ]
        },
        "Person": {
            "required": [
                "name"
            ],
            "properties": {
                "name": {
                    "type": "string"
                }
            }
        },
        "Address": {
            "required": [
                "postalCode",
                "city"
            ],
            "properties": {
                "street": {
                    "type": "string"
                },
                "postalCode": {
                    "type": "integer"
                },
                "city": {
                    "type": "string"
                }
            }
        }
    }
}

Activity

  1. wing328 commented on Jun 30, 2015

    @wing328
    Contributor

    @McFoggy I'm able to repeat the issue. Based on some testing, seems like codegen is expecting the model to have "properties". If I update Client as follows, I could no longer repeat the issue:

        "definitions": {
            "Client": {
                "allOf": [
                    {
                        "$ref": "#/definitions/Person"
                    },
                    {
                        "$ref": "#/definitions/Address"
                    },
                    {
                      "properties": {
                        "tmp_name": {
                          "type": "string"
                        }
                      }
                    }
                ]
            },
    

    I wonder if you can try adding the same (a property) to see if the issue goes away.

    This is not to say it's an acceptable workaround but to confirm codegen is expecting a model to have at least one property defined.

  2. McFoggy commented on Jun 30, 2015

    @McFoggy
    Author

    I confirmed that your workaround worked when I added a dummy property at all places where I used 'only' allOf for composition.

  3. wing328 commented on Jun 30, 2015

    @wing328
    Contributor

    @McFoggy Thanaks. I'll look into the issue and hopefully can find a fix tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions