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

How to specify $ref base url? #20

Closed
Alain1405 opened this issue Jun 13, 2016 · 6 comments
Closed

How to specify $ref base url? #20

Alain1405 opened this issue Jun 13, 2016 · 6 comments

Comments

@Alain1405
Copy link

Alain1405 commented Jun 13, 2016

I serve a frontend locally with node at localhost:8000. I pull the json schema from an API.
The backend returning the json schema seats at localhost:8888 (http:https://127.0.0.1:8888/api/controllers/schema).

The schema has arrays of $ref fields in form of relative urls :
var schema = [{$ref:"/api/controllers/123"},{...}].

json-schema-ref-parser resolves references relatively to the frontend url rather than the backend url. $RefParser.resolve(schema); throws the following errors:

  • request.js:114 GET http:https://localhost:8000/api/controllers/123 404 (Not Found)
  • index.js:67 Uncaught (in promise) Error: Error downloading http:https://localhost:8000/api/controllers/123.

How can I pass localhost:8888 to resolve as base url?

@Alain1405 Alain1405 changed the title How to specify $ref url? How to specify $ref base url? Jun 14, 2016
@Alain1405
Copy link
Author

I've just realized that I can pass a url to the schema to resolve, in which case the $refs are resolved correctly, but yet this is not what I want. I pull the schema using AngularJs $reource service, which allows queries, sorting and pagination. Then I want to pass the resulting schema to the resolve method together with the base url of the api.

@Alain1405
Copy link
Author

Here is an example of schema passed to refParser:

var json = [{
    "$uri": "/api/users/4",
    "controllers": [
        {"$ref": "/api/controllers/123"},
        {"$ref": "/api/controllers/456"},
        {"$ref": "/api/controllers/678"}
    ],
    "email": "[email protected]",
    "is_deleted": false,
    "is_super_admin": true,
    "is_verified": true
}, {
    "$uri": "/api/users/13",
    "controllers": [
        {"$ref": "/api/controllers/1233"},
        {"$ref": "/api/controllers/4231"}
    ],
    "email": "[email protected]",
    "is_deleted": false,
    "is_super_admin": false,
    "is_verified": true
}];

@JamesMessinger
Copy link
Member

Sorry I didn't get a chance to respond yesterday, but it looks like you've discovered what I was going to say anyway. The trick is to pass the URL that you want (in your case http:https://localhost:8888) along with the schema object.

@dzuluaga
Copy link

dzuluaga commented Aug 27, 2016

Same problem here. I get this error Error: ENOENT: no such file or directory, open '/Documents/tools/git/dir1/dir2/node/refs/parameters.json' with a JSON file with a ref like this one below:

/** this file is located under /node/api/models/openapi-model.json and refs 
*    are located under refs of the same directory. e.g. /node/api/models/refs/parameters/json
*    app.js is located under node folder /node/app.js, which uses json-schema-ref-parser to load 
*    the json file as this jsonRefs.dereference('./api/models/openapi-model.json')
**/  
{
  "resources": {
    "collection": {
      "description": "A collection of Accounts.",
      "parameters": [
        {
          "$ref": "./refs/parameters.json#/common/collection"
        },
        {
          "in": "query",
          "name": "customer",
          "description": "Only include customers (stage 6 in SFDC)",
          "required": false,
          "type": "boolean"
        }
      ]
    }
  }
}

The only way to workaround this issue is to move my refs directory to the current working directory or essentially, the root directory of my Node.js app, which I'm trying to avoid as it breaks a component-based structure. I'm also trying to avoid http protocol to fetch those files.

It'd be great if as part of the options, there was a way to specify a relative base.

BTW - Love the project!

@JamesMessinger
Copy link
Member

@dzuluaga - See my reply above. You can specify the base URL as a parameter.

@dzuluaga
Copy link

Awesome @BigstickCarpet. It was a bit cryptic ;). But finally worked. Issue #13 gave me the clues. Here's my version of the above.

  return $RefParser.dereference('./api/models/', './api/models/openapi-model.json', {})
      .then(function(dmResolved) {
        debug('data model resolved', dmResolved);
        return dmResolved;
      })

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

4 participants