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

Recursive model errors during flattening stage of generate server #1796

Closed
kstedman9 opened this issue Nov 16, 2018 · 2 comments · Fixed by #1799
Closed

Recursive model errors during flattening stage of generate server #1796

kstedman9 opened this issue Nov 16, 2018 · 2 comments · Fixed by #1799

Comments

@kstedman9
Copy link

kstedman9 commented Nov 16, 2018

Problem statement

I have a recursive model that is separated into a models file that is referred to by "$ref" tag. When I try to generate the server it panics during the flattening process. So you can also test by just running the flattening. If I remove the recursion it works just fine. To remove recursion I just change in the QueryNode model all "$ref": "#/QueryNode" to "type": "object"

Part of the output messages with debug turned on.

analysis:2018/11/16 09:14:15 flatten.go:803: importing external schema for [#/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/and/items, #/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/or/items, #/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/not/items] from /home/kstedman/projects/develop/swag_issue/models/query.json#/QueryNode
analysis:2018/11/16 09:14:15 flatten.go:807: new name for [#/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/and/items, #/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/or/items, #/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/not/items]: queryNode - with name conflict:false
analysis:2018/11/16 09:14:15 flatten.go:950: updating ref for #/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/and/items with #/definitions/queryNode
analysis:2018/11/16 09:14:15 flatten.go:950: updating ref for #/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/or/items with #/definitions/queryNode
analysis:2018/11/16 09:14:15 flatten.go:950: updating ref for #/paths/~1search/post/parameters/0/schema/properties/queryRoot/properties/not/items with #/definitions/queryNode
analysis:2018/11/16 09:14:15 flatten.go:1260: name pointers
analysis:2018/11/16 09:14:15 flatten.go:1272: planning pointer to replace at #/definitions/queryNode/properties/or/items: models/query.json#/QueryNode, resolved to: models/query.json#/QueryNode
analysis:2018/11/16 09:14:15 flatten.go:1272: planning pointer to replace at #/definitions/queryNode/properties/and/items: models/query.json#/QueryNode, resolved to: models/query.json#/QueryNode
analysis:2018/11/16 09:14:15 flatten.go:1272: planning pointer to replace at #/definitions/queryNode/properties/not/items: models/query.json#/QueryNode, resolved to: models/query.json#/QueryNode
analysis:2018/11/16 09:14:15 flatten.go:1298: replacing pointer at #/definitions/queryNode/properties/and/items: resolved to: models/query.json#/QueryNode
analysis:2018/11/16 09:14:15 flatten.go:1314: namePointers at #/definitions/queryNode/properties/and/items for models/query.json#/QueryNode
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x1a0 pc=0x7dab09]

goroutine 1 [running]:
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.(*AnalyzedSchema).initializeFlags(0xc0005e21c0)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/schema.go:213 +0x29
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.Schema(0x0, 0xb32600, 0xc0002cc700, 0xc000690c40, 0x3a, 0x0, 0xc00046a580, 0xc0007868e0, 0x1)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/schema.go:25 +0x97
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.namePointers(0xc0006e8330, 0xc0007799a0, 0x7bae49)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/flatten.go:1317 +0xed1
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.stripPointersAndOAIGen(0xc0006e8330, 0x0, 0x0)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/flatten.go:1130 +0x2f
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.Flatten(0xc000695180, 0x0, 0x7ffe431501cb, 0xf, 0x10101, 0x8, 0x7)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/flatten.go:182 +0x2e5
github.com/go-swagger/go-swagger/cmd/swagger/commands.(*FlattenSpec).Execute(0xc00008e690, 0xc00039f080, 0x1, 0x6, 0xc00008e690, 0x1)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/cmd/swagger/commands/flatten.go:43 +0x18f
github.com/go-swagger/go-swagger/vendor/github.com/jessevdk/go-flags.(*Parser).ParseArgs(0xc00039f020, 0xc00001e080, 0x6, 0x6, 0x40dac8, 0x30, 0xb7ba60, 0xc000092990, 0xc000431b00)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/vendor/github.com/jessevdk/go-flags/parser.go:316 +0x800
github.com/go-swagger/go-swagger/vendor/github.com/jessevdk/go-flags.(*Parser).Parse(0xc00039f020, 0x6, 0xbcba2a, 0x6, 0x0, 0xc16ff0)
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/vendor/github.com/jessevdk/go-flags/parser.go:186 +0x71
main.main()
	/home/kstedman/go/src/github.com/go-swagger/go-swagger/cmd/swagger/swagger.go:140 +0xb68

Swagger specification

So I have a swagger json definition. The top level file contains the paths. Models are found in a subfolder models. FYI this is a small part of a very large swagger project.

./queryIssue.json

{
  "swagger": "2.0",
  "info": {
    "title": "Query Model Issue",
    "description": "Test for Query Model Issue",
    "version": "0.0.1"
  },
  "schemes": ["http"],
  "produces": ["application/json"],
  "paths": {
 
    "/search": {
      "post": {
        "operationId": "searchObject",
        "tags": ["Query"],
        "produces": ["application/json"],
        "consumes": ["application/json"],
        "security": [
          {
            "Bearer": []
          }
        ],
        "summary":
          "Get a list of the algorithms available in the AlgorithmBank instance. By default returns the first 10 results.",
        "parameters": [
          {
            "name": "query",
            "in": "body",
            "required": true,
            "schema": {
              "$ref": "./models/query.json#/Query"
            }
          }
        ], 
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "string"
            }
          },
          "400": {
            "description": "Invalid parameters."
          },
          "401": {
            "description": "Unauthenticated."
          },
          "403": {
            "description": "User is not authorized to perform the action",
            "schema": {
              "type": "string"
            }
          },
          "500": {
            "description": "Internal Server Error",
            "schema": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "definitions": {

  }
}

./models/query.json:

{
    "Query": {
       "description":
         "Represents a search query using a boolean expression tree",
       "type": "object",
       "properties": {
         "queryRoot": {
           "$ref": "#/QueryNode"
         },
         "limit": {
           "type": "integer",
           "format": "int64"
         },
         "offset": {
           "type": "integer",
           "format": "int64"
         }
       },
       "x-go-type": {
           "import": {
              "package": "bitbucket.di2e.net/scm/pir/ab-models.git",
              "alias": "models"
           },
           "type": "Query"
       }
     },
     "QueryNode": {
      "description":
        "Node in a boolean expression tree that represents a query",
      "type": "object",
      "properties": {
        "and": {
          "type": "array",
          "items": {
            "$ref": "#/QueryNode"
          }
        },
        "or": {
          "type": "array",
          "items": {
            "$ref": "#/QueryNode"
          }
        },
        "not": {
          "type": "array",
          "items": {
            "$ref": "#/QueryNode"
          }
        },
        "wildcards": {
          "description":
            "Array of key/value pairs representing wildcard term matches",
          "type": "array",
          "items": {
            "$ref": "./pair.json#/Pair"
          }
        },
        "exact": {
          "description":
            "Array of key/value pairs representing exact term matches",
          "type": "array",
          "items": {
            "$ref": "./pair.json#/Pair"
          }
        }
      },
      "x-go-type": {
          "import": {
            "package": "bitbucket.di2e.net/scm/pir/ab-models.git",
            "alias": "models"
          },
          "type": "QueryNode"
      }
    }
 }

./models/pair.json:

{
    "Pair": {
        "description":
          "Simple key value pair model that is used by some of the other models.",
        "type": "object",
        "required": ["key", "value"],
        "properties": {
          "key": {
            "type": "string"
          },
          "value": {
            "type": "string"
          }
        },
        "x-go-type": {
            "import": {
              "package": "bitbucket.di2e.net/scm/pir/ab-models.git",
              "alias": "abmodels"
            },
            "type": "Pair"
        }
     }
  }

Steps to reproduce

Run: swagger flatten -o queryIssueFlatten.json --format=json --with-expand queryIssue.json
or
Run: swagger generate server -f queryIssue.json -A queryissue

Both commands give the same issue

Environment

swagger version: 0.17.2 and master branch
go version: 1.10.x, 1.11.x
OS: Centos 7

Attached are all the json files and the complete log output.
issue.zip

@fredbi
Copy link
Contributor

fredbi commented Nov 16, 2018

Could reproduce in go-openapi/analyis. There are still some cyclic ref patterns that cause some bugs...

fredbi added a commit to fredbi/analysis that referenced this issue Nov 16, 2018
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/analysis that referenced this issue Nov 16, 2018
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi
Copy link
Contributor

fredbi commented Nov 16, 2018

I think I solved the issue in go-openapi analysis.
Some feedback would be much valuable: go-openapi/analysis#40

fredbi added a commit to fredbi/analysis that referenced this issue Nov 16, 2018
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi fredbi self-assigned this Nov 16, 2018
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 16, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 16, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 17, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
casualjim added a commit to go-openapi/analysis that referenced this issue Nov 17, 2018
Fix go-swagger/go-swagger#1796 (circular $ref with cycle in remote)
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 17, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 18, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 19, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 22, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 28, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit that referenced this issue Nov 28, 2018
* Fix flattening spec with remote circular $ref

* fixes #1796
* fixes #1774
* fixes #1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* * Updates vendor (20181118)

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants