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

Bundle replaces duplicate $refs with relative pointers #101

Closed
philosowaffle opened this issue Sep 15, 2018 · 2 comments
Closed

Bundle replaces duplicate $refs with relative pointers #101

philosowaffle opened this issue Sep 15, 2018 · 2 comments

Comments

@philosowaffle
Copy link

First and foremost, this may be a non-issue, I just wanted to confirm the behavior was intended and verify that I am using the library correctly. This also may not be the correct repo for this issue.

I had an issue logged where a user was getting errors in their swagger doc after using multiple $refs to the same file. Example Schema

The following test code demonstrates the issue using the linked schema:

const refParser = require('json-schema-ref-parser')
const path = require('path');

var source = path.join(__dirname, '/schema/petstore.yaml');

refParser.bundle(source, (err, schema) => {
    if (err) {
        console.error('ERROR:', err);
    }
    else {
       // at this point swagger editor claims the schema is invalid
        console.log('SUCCESS:', JSON.stringify(schema, null, 2)); 
        console.log("DEREFERENCE");

        refParser.dereference(schema, (err, drSchema) => {
            if (err) {
                console.error('ERROR:', err);
            }
            else {
               // after a dereference, swagger editor says schema is valid
                console.log('SUCCESS:', JSON.stringify(drSchema, null, 2)); 
            }
        });
    }
});

An initial call to bundle creates a schema that uses local json pointers for the duplicate references. However, this throws errors when copy/pasted into the official Swagger Editor. I noticed that this same bundled schema does not yield any errors when plugged into your Swagger Validator Demo.

After reading through this issue I determined that you must be calling dereference before doing the validation. When I add a follow up call to dereference, as seen in the test above, the relative json pointers are dereferenced and swagger editor is happy again.

  1. Is this intended behavior?
  2. Is calling bundle followed by dereference a suitable workaround, or should I be doing something else in entirely?
@JamesMessinger
Copy link
Member

Hi @philosowaffle. Welcome to the wonderful world of incompatible Swagger tooling! 😄

Unfortunately, as you've discovered, not all Swagger & OpenAPI tools are compatible. Many of them don't fully support the spec, and there are a few areas where the spec is unclear so different tooling authors have implemented it differently.

Swagger Editor is a fantastic tool, but its $ref support is somewhat lacking. This has led to other issues in the past. For now, the only workaround is to use dereference() rather than bundle(), since dereference() produces a file without any $refs, so it's guaranteed to work in Swagger Editor.

@philosowaffle
Copy link
Author

Thanks!

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

2 participants