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

Error: can't resolve reference #/definitions/User from id # #9

Closed
raghavchandra opened this issue Oct 20, 2017 · 4 comments
Closed

Comments

@raghavchandra
Copy link

raghavchandra commented Oct 20, 2017

In my schema, I have a definitions section which I use. It seems like it is not able to correctly parse the $ref in this case (probably because it doesnt fine an ID).
However, the below is a valid schema that should work. Anything that I am missing?

I get back –

{ Error: can't resolve reference #/definitions/User from id #
    at Object.generate_ref [as code] (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/ajv/lib/dotjs/ref.js:61:22)
    at generate_validate (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/ajv/lib/dotjs/validate.js:155:37)
    at localCompile (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/ajv/lib/compile/index.js:99:22)
    at Ajv.compile (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/ajv/lib/compile/index.js:67:13)
    at _compile (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/ajv/lib/ajv.js:328:29)
    at Ajv.compile (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/ajv/lib/ajv.js:114:34)
    at validateRequest (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/swagger-express-validator/index.js:205:27)
    at validate (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/swagger-express-validator/index.js:236:5)
    at Layer.handle [as handle_request] (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/raghav/Documents/work/urbanclap/grpc-framework-node/node_modules/express/lib/router/index.js:317:13) 
missingRef: '#/definitions/User', missingSchema: '' }

Schema –

{
  "swagger": "2.0",
  "info": {
    "description": "This is where you write the description of your service.",
    "version": "1.0.0",
    "title": "Example"
  },
  "paths": {
    "/users": {
      "post": {
        "summary": "Creates a new user.",
        "consumes": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "The user to create.",
            "required": true,
            "schema": {
              "$ref": "#/definitions/User"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "object",
              "required": [
                "userName"
              ],
              "properties": {
                "userName": {
                  "type": "string"
                },
                "firstName": {
                  "type": "string"
                },
                "lastName": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "User": {
      "type": "object",
      "required": [
        "userName"
      ],
      "properties": {
        "userName": {
          "type": "string"
        },
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        }
      }
    }
  }
}

Thanks

@iadknet
Copy link

iadknet commented Oct 20, 2017

I use this project to expand swagger refs:

https://github.com/duncanhall/expand-swagger-refs

Here is a simple script I threw together that creates a http proxy that validates against a swagger doc (and expands the refs):

https://gist.github.com/iadknet/7a908e9d7cd254cb7025a9d995ccaada

@naz
Copy link
Owner

naz commented Oct 21, 2017

@raghavchandra there used to be this https://github.com/gargol/swagger-express-validator/blob/24a83660fc33708e8938faf754c7d9e4870bc67c/test/helpers/schema-resolver.js simple schema resolving script, but I got rid of it to have less noise in the project

@raghavchandra
Copy link
Author

raghavchandra commented Oct 21, 2017

@gargol @iadknet thanks. The expand-swagger-refs works well too.
what I like about swagger-express-validator is that it is just a middle-ware plugin getting defined in sync – since most of the simpler express projects follow a sync app.js definition, it keeps the format the same – other parsers / validators either have a different way of routing, or an async mechanism making it a little complicated.
[off topic] Btw, any scope for you extending it to support OpenAPI 3.0 spec?

@naz
Copy link
Owner

naz commented Oct 23, 2017

@raghavchandra regarding 3.0 spec support, I can't promise any dates, as this project was done during my spare time. I hope to do more research on the new spec and improve this package during my winter vacations (mid-December till January)

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

3 participants