Skip to content

v3 - Resolver error - Cannot read property '1' of undefined #3366

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

Closed
alrik opened this issue Jul 11, 2017 · 16 comments
Closed

v3 - Resolver error - Cannot read property '1' of undefined #3366

alrik opened this issue Jul 11, 2017 · 16 comments

Comments

@alrik
Copy link

alrik commented Jul 11, 2017

This error occured when we upgraded to swagger ui 3.x.
We use [email protected].

The issue can be reproduced with the following swagger-spec on Swagger Editor:

# this is an bug report spec
swagger: '2.0'
info:
  title: Swagger UI 3 Bug Report
  description: |
    This will display the following error:
  
    ## Resolver error
    
    `Cannot read property '1' of undefined`
  version: 1.0.0
host: localhost
schemes:
  - https
basePath: /v1
produces:
  - application/json
paths:
  /resolver-error:
    get:
      tags:
        - Bug Report
      responses:
        200:
          description: The trouble maker
          schema:
            $ref: '#/definitions/foo-bar'

definitions:
  foo-bar:
    allOf:
      - $ref: '#/definitions/baz'
      - properties:
          data:
            type: array
            items:
              $ref: '#/definitions/foo'

  foo:
    allOf:
      - $ref: '#/definitions/bar'
      - properties:
          foo:
            type: string

  bar:
    properties:
      bar:
        type: string
      foo:
        $ref: '#/definitions/foo'

  baz:
    properties:
      baz:
        type: string
@webron
Copy link
Contributor

webron commented Jul 11, 2017

Yeah, this is due to the circular reference.

@alrik
Copy link
Author

alrik commented Jul 12, 2017

This could be related to: #3051

@ajo2995
Copy link

ajo2995 commented Jan 15, 2018

I have the same problem defining a tree structure. It's very disconcerting when the first thing a user sees at http://www.genetrees.org is an obscure error message. Is there a way to suppress the error message?

See the TreeNode definition here

@jeff9finger
Copy link

@webron Does your comment above mean that circular references are not allowed?
Is there a way to allowCircular or ignore the error?

Our spec requires the use of a circular reference.

@jeff9finger
Copy link

Seems like the error might be: swagger-api/swagger-js#1077

@stoutfiles
Copy link

stoutfiles commented Feb 5, 2018

Need to be able to remove error messages. For whatever reason, it might not be something that's easily fixable, or just something that the resolver can't handle correctly.

@heldersepu
Copy link
Contributor

@stoutfiles if hiding the error is all want, try a custom css:

.errors-wrapper {
    display: none !IMPORTANT;
}

@shockey
Copy link
Contributor

shockey commented Feb 5, 2018

@heldersepu, good thinking!

@stoutfiles, alternatively, if modifying JS is more your speed, here are a pair of Swagger-UI plugins that will hide resolver errors or hide all errors:

  // hide errors that come from the resolver
  const HideResolverErrorsPlugin = () => {
    return {
      statePlugins: {
        err: {
          wrapSelectors: {
            allErrors: (ori) => () => {
              return ori().filter(err => err.get("source") !== "resolver")
            }
          }
        }
      }
    }
  }

  // hide the entire Errors container
  const HideAllErrorsPlugin = () => {
    return {
      wrapComponents: {
        errors: () => () => null
      }
    }
  }

@jeff9finger
Copy link

jeff9finger commented Feb 7, 2018

We added the suggested plugin above, and it gets around the immediate problem. I'd like to see something in swagger-js that will not cause an error for this particular case in the first place.

@ralfhandl
Copy link

ralfhandl commented Feb 14, 2018

I've got a similar problem with

Resolver error: Cannot read property 'items' of undefined

Note: my problem was solved with Swagger UI 3.11.0, example now works fine.

@scottohara
Copy link
Contributor

scottohara commented Feb 15, 2018

One of our users has hit this issue, and interestingly it only seems to occur if the object with the circular reference (Item) is itself referenced by another object (List).

To explain, here's an example that causes a Resolver error: Cannot read property 'children' of undefined:

{
  "swagger": "2.0",
  "info": {
    "title": "Test API"
  },
  "basePath": "/",
  "paths": {
    "/test": {
      "get": {
        "responses": {
          "200": {
            "schema": {
              "$ref": "#/definitions/List"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "List": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Item"
      }
    },
    "Item": {
      "type": "object",
      "properties": {
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Item"
          }
        }
      }
    }
  }
}

And here is a functionally equivalent version, that doesn't exhibit the error.

The only difference is simply that the GET /test "200 OK" response schema directly references an array of Item objects, instead of via the interim List definition:

{
  "swagger": "2.0",
  "info": {
    "title": "Test API"
  },
  "basePath": "/",
  "paths": {
    "/test": {
      "get": {
        "responses": {
          "200": {
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Item"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "Item": {
      "type": "object",
      "properties": {
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Item"
          }
        }
      }
    }
  }
}

@scottohara
Copy link
Contributor

Actually, it may not be the circular reference at all. I suspect it is something to do with the number of levels being traversed when resolving.

Here is another reduced test case that also gets a Resolver error: Cannot read property 'items' of undefined, but this time there is no circular reference.

open_api_customer_support_-test_space-_confluence

In total, there are 4 levels to get down to the leaf Item. Collapsing any one of these (down to 3 levels) seems to fix the problem, e.g.

  1. Remove #/parameters/list, and inline the parameter definition into the PUT /list operation, or
  2. Remove #/definitions/Items, and inline the array definition into #/definitions/List
  3. Remove #/definitions/Item, and inline the the object definition into the array items in #/definitions/Items
{
  "swagger": "2.0",
  "paths": {
    "/list": {
      "put": {
        "parameters": [
          {
            "$ref": "#/parameters/list"
          }
        ]
      }
    }
  },
  "parameters": {
    "list": {
      "name": "list",
      "in": "body",
      "schema": {
        "$ref": "#/definitions/List"
      }
    }
  },
  "definitions": {
    "List": {
      "type": "object",
      "properties": {
        "items": {
          "$ref": "#/definitions/Items"
        }
      }
    },
    "Items": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Item"
      }
    },
    "Item": {
      "type": "object",
      "properties": {
        "itemId": {
          "type": "string"
        }
      }
    }
  }
}

@ElMicko
Copy link

ElMicko commented Feb 19, 2018

When working with a similar example I found that removing forward references in the definitions removed the error. ie declare 'Item' then 'Items' then 'List'. Same definitions.. just change in order.

Obviously this is not a real fix as you want to order your object definitions in a more accessible manner but perhaps it might assist in locating the issue in the resolver code? Perhaps the issue is the number of forward references, as @scottohara points out, collapsing references into inline definitions also removes the error message.

Here is the above definition from @scottohara reordered, and not showing the unexpected errror.

{
  "swagger": "2.0",
  "info": {
    "version": "0",
    "title": "No forward references",
    "description": "sample"
  },
  "paths": {
    "/list": {
      "put": {
        "parameters": [
          {
            "$ref": "#/parameters/list"
          }
        ],
        "responses": {
          "200": {
            "$ref": "#/responses/OK"
          }
        }
      }
    }
  },
  "parameters": {
    "list": {
      "name": "list",
      "in": "body",
      "schema": {
        "$ref": "#/definitions/List"
      }
    }
  },
  "responses": {
    "OK": {
      "description": "200 OK"
    }
  },
  "definitions": {
    "Item": {
      "type": "object",
      "properties": {
        "itemId": {
          "type": "string"
        }
      }
    },
    "Items": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Item"
      }
    },
    "List": {
      "type": "object",
      "properties": {
        "items": {
          "$ref": "#/definitions/Items"
        }
      }
    }
  }
}

no forward

@scottohara
Copy link
Contributor

This PR (swagger-api/swagger-js#1243) seems to also confirm that order of references can workaround the issue (definitions before paths; and within definitions, declaration before use).

@scottohara
Copy link
Contributor

I'm happy to report that Swagger UI v3.12.0 (presumably this specific fix: #4273) now correctly resolves two examples above that were previously failing with Resolver error: Cannot read property '<prop>' of undefined (#3366 (comment) and #3366 (comment))

@webron
Copy link
Contributor

webron commented Jul 5, 2018

Closing as resolved.

@webron webron closed this as completed Jul 5, 2018
@lock lock bot locked and limited conversation to collaborators Jul 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants