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

internal reference in schema #19

Closed
mailingmuthu opened this issue May 23, 2016 · 11 comments
Closed

internal reference in schema #19

mailingmuthu opened this issue May 23, 2016 · 11 comments

Comments

@mailingmuthu
Copy link

mailingmuthu commented May 23, 2016

I am trying to dereference resolved schema object. But it is throwing error as "Error resolving $ref pointer "#/definitions/ApiCallList". Token "definitions" does not exist."

Is this a bug or What is the correct way to define $ref to refer internal definition within the schema object.

$RefParser = require('json-schema-ref-parser') $RefParser.dereference(schema, { $refs: { circular: "ignore" } }) .then(function(resolvedSchema) { res.send(resolvedSchema); })

Sample Schema
{ "definitions":{ "ApiCallList":{ "description":"A list of Api Call Resources", "id":"ApiCallList" }, "MaskCallList":{ "description":"A list of Mask Call Resources", "id":"MaskCallList" } }, "resources":{ "api-call":{ "methods":{ "request":{ "$ref":"#/definitions/ApiCallList }, "response":{ "$ref":"#/definitions/MaskCallList } } } } }

@JamesMessinger
Copy link
Member

hmmm... Maybe there's just a typo or something? It runs fine for me. Try it out here

@mailingmuthu
Copy link
Author

May i know which version of json-schema-ref-parser are you using here ?

@JamesMessinger
Copy link
Member

The code sample I posted is using the latest released version (3.1.2), but it would also work with any previous version. For example, here it is working with version 1.0.0

@dave-irvine
Copy link

dave-irvine commented Dec 23, 2016

I think I'm having a similar issue.

Here's my main swagger.yaml:

swagger.yaml

{
  "swagger": "2.0",
  "info": {
    "version": "0.0.1",
    "title": "User API"
  },
  "definitions": {
    "User": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  },
  "paths": {
    "$ref": "./paths.yaml"
  }
}

which references paths.yaml:

paths.yaml

/users:
  get:
    responses:
      200:
        description: OK
        schema:
          $ref: '#/definitions/User'

and when I try to bundle them:

bundler.js

var $RefParser = require('json-schema-ref-parser');
var util = require('util');

$RefParser.bundle('swagger.yaml',function(err, schema) {
  if (err) {
    console.error(err);
  }
  else {
    console.log(util.inspect(schema, {depth:null}));
  }
});

Result:

{
  "name": "SyntaxError",
  "message": "Error resolving $ref pointer \"~/swagger.yaml#/definitions/User\".
Token \"definitions\" does not exist.",
  "stack": "SyntaxError: Error resolving $ref pointer \"~/swagger.yaml#/definitions/User\".
Token \"definitions\" does not exist.
    at Function.ono [as syntax] (~/node_modules/ono/lib/index.js:67:20)
    at Pointer.resolve (~/node_modules/json-schema-ref-parser/lib/pointer.js:73:17)
    at $Ref.resolve (~/node_modules/json-schema-ref-parser/lib/ref.js:82:18)
    at $Refs._resolve (~/node_modules/json-schema-ref-parser/lib/refs.js:157:15)
    at inventory$Ref (~/node_modules/json-schema-ref-parser/lib/bundle.js:99:23)
    at ~/node_modules/json-schema-ref-parser/lib/bundle.js:69:11
    at Array.forEach (native)
    at crawl (~/node_modules/json-schema-ref-parser/lib/bundle.js:63:12)
    at ~/node_modules/json-schema-ref-parser/lib/bundle.js:72:11
    at Array.forEach (native)"
}

@dave-irvine
Copy link

Happy New Year @BigstickCarpet! I'm wondering if you've had the opportunity to check this out? No problem if not, I know its a busy time :)

@JamesMessinger
Copy link
Member

Hi, @dave-irvine. Sorry, I somehow missed your comment from two weeks ago; otherwise, I would have responded sooner.

The behavior you're seeing is by design and per the JSON Pointer spec. JSON pointers are relative to the root of the document they're in. So, in paths.yaml, the $ref to #/definitions/User is invalid. You should replace it with swagger.yaml#/definitions/User instead.

@dave-irvine
Copy link

Aha! Thanks very much :)

@pici-masp
Copy link

@BigstickCarpet I just ported my code to using your library for validation and bundle. I experienced the same problem as @dave-irvine .

I am sure swagger.yaml#/definitions/User is the right way to go but if you have a root swagger.yaml and $ref for definitions and path files that are also in subfolders this is not ideal. I my case I would have to do something like ../../swagger.yaml#/definitions/User. Since also the definitions might not be present yet because they are also referenced in the root yaml, this would be another problem right?

I used require('json-refs').resolveRefs before which allows me to divide definition and path files in a clean folder structure and it accepts (ignores) internal #/definitions/User references in files that do not have definitions itself but later are present when "bundled".

I am no pro regarding swagger but it is great to have the freedom to use this kind of internal definition references in subfolder files that do not have these itself. What do you say? Is there a better way?

An option like {ignoreDefinitionReferenceNotFound: true} or somthing similar would solve my special case.

Any way. Great library. Happy Swaggering. 💃

@pici-masp
Copy link

Hi @BigstickCarpet again. An optional error handler would be a nice solution.
bundle: {onReferenceError: (err) => return true}
If true ref will be ignored else bundle throws error.

@mrsombre
Copy link

This is a problem code

if (value.$ref.substr(0, 2) === '#/' || value.$ref === '#') {
      // It's a JSON Pointer reference, which is always allowed
      return true;
}

Need a PR adds option to skip this type references resolution (starting exactly with #).
Ex: #/definitions/User

@JamesMessinger
Copy link
Member

As far as I can tell, everything is behaving as expected and according to the spec. Perhaps I don't understand something. Can somebody please provide a reproduction of the issue, preferably on RunKit?

Here are reproductions of other issues, as examples:

https://runkit.com/bigstickcarpet/json-schema-ref-parser-issue-93

https://runkit.com/bigstickcarpet/swagger-cli-issue-19

https://runkit.com/bigstickcarpet/swagger-parser-issue-90

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

No branches or pull requests

6 participants