Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Cannot resolve $ref when $ref points to a object within a same file. #494

Closed
FieryCod opened this issue Apr 4, 2017 · 12 comments
Closed

Comments

@FieryCod
Copy link

FieryCod commented Apr 4, 2017

I'm using code from @whitlockjc which perfectly resolves relative paths and I am really happy with it :)

   JsonRefs.resolveRefsAt('./api/swagger/swagger.yaml', {
      // Resolve all remote references
      filter: ['relative', 'remote'],
      loaderOptions: {
        processContent(res, cb) {
          cb(undefined, YAML.safeLoad(res.text));
        },
      },
    })
    .then((results) => {
      swaggerTools.initializeMiddleware(results.resolved, (middleware) => {
        // Wire up the Swagger middleware
        // Interpret Swagger resources and attach metadata to request
        // - must be first in swagger-tools middleware chain
        app.use(middleware.swaggerMetadata());

        // Validate Swagger requests
        app.use(middleware.swaggerValidator({ validateResponse: true }));

        // Route validated requests to appropriate controller
        app.use(middleware.swaggerRouter(options));

        // Serve the Swagger documents and Swagger UI
        app.use(middleware.swaggerUi({
          swaggerUi: '/api/docs/',
          apiDocs: '/api/swagger/swagger.yaml',
        }));
      });
    })
    .catch((err) => {
      console.error(err.stack);
      process.exit(1);
    });

There is only one problem when I define some schema in file /models/Article.yaml and then point to that schema using $ref in Article I receive an error. (except from swagger.yaml in which pointing to definitions resolves correctly)

Error message:

#/paths/1articles1{id}/get/responses/200/schema/properties/content/$ref: Definition could nZW50LyRyZWY6IERlZmluaXRpb24gY291bGQgbm90IGJlIHJlc29sdmVkOiAjL2RlZmluaXRpb25zL0FydGljbGVDb250ZW50 could not be resolved: #/definitions/ArticleContent

Article.yaml

Article:
  type: object
  required:
    - somethingRequired
    - id
    - createdAt
    - title
    - content
    - author
  properties:
    id:
      type: integer
    createdAt:
      type: string
      format: date
    title:
      type: string
    content:
      # TODO: Fix relative
      # $ref: '#/ArticleContent'
      $ref: '#/definitions/ArticleContent' <--- Here I receive an error
    author:
      type: string
    somethingRequired:
      type: string
definitions:
  ArticleContent:
    type: object
    required:
      - text
    properties:
      text:
        description: Main text/html content of article
        type: string

I would highly appreciate your help with that issue.

Link to that issue

@whitlockjc whitlockjc reopened this Apr 4, 2017
@whitlockjc
Copy link
Member

whitlockjc commented Apr 4, 2017

It seems like you've run into whitlockjc/json-refs#80 json-refs should be released by EOW and once that's done, swagger-tools and sway will see a release as well.

@FieryCod
Copy link
Author

FieryCod commented Apr 4, 2017

EOW? So currently there is no other way then referencing to local definitions with url?

@whitlockjc
Copy link
Member

EOW === "End of Week". The problem is due to a bug in json-refs. When that is fixed, your issue will be fixed.

@FieryCod
Copy link
Author

FieryCod commented Apr 4, 2017

Aaa ok :) Thanks for your work and fast response. I m looking forward to a next release of json-refs. Hope that in the next week it will be resolved. I'm gonna close that issue after update of json-refs.

@fmauNeko
Copy link

Just ran into this bug, it has been fixed with json-refs 2.1.7.
Upgrade your deps and you should be golden ;)

@whitlockjc
Copy link
Member

json-refs@v2.1.7 should not fix this. If you look at the commits for json-refs@v2.1.7, it's just for updating the dependencies to fix security issues: https://github.com/whitlockjc/json-refs/commits/v2.1.7 The upcoming json-refs@v3.0.0 should fix this and it will be out today.

@fmauNeko
Copy link

Weird, because it fixed it for me actually, didn't work for me @v2.1.6, worked @v2.1.7

@whitlockjc
Copy link
Member

That is super weird. :)

@FieryCod
Copy link
Author

Issue resolved by updating json-refs to the newest json-refs3.0.0. If you someone want to use relative paths with swagger-tools this script should work fine: (author @whitlockjc )

   JsonRefs.resolveRefsAt('./api/swagger/swagger.yaml', {
      // Resolve all remote references
      loaderOptions: {
        processContent(res, cb) {
          cb(undefined, YAML.safeLoad(res.text));
        },
      },
    })

Make sure to not use filter option in order to get fully resolved references.

@whitlockjc
Copy link
Member

But this was also be fixed when swagger-tools is updated to json-refs@v3.0.0.

@FieryCod
Copy link
Author

You mean that swagger-tools will support resolving relative .yaml paths by default?

@whitlockjc
Copy link
Member

I'm not sure yet, but I might be able to add a new initializer method that takes the necessary pieces of information to make it work right. The biggest part of swagger-tools that I cannot change to make it work is the API, but the middleware initializer I might be able to figure something out.

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

No branches or pull requests

3 participants