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

Ability to skip dereferencing inline/internal $ref values #214

Closed
denver-HJS opened this issue Feb 4, 2021 · 6 comments
Closed

Ability to skip dereferencing inline/internal $ref values #214

denver-HJS opened this issue Feb 4, 2021 · 6 comments

Comments

@denver-HJS
Copy link

denver-HJS commented Feb 4, 2021

Hi all,

@stoplight/json-ref-resolver offers options to skip dereferencing either internal or remote references. I've found this feature to be very useful, but now that project is now deprecated and points to this one.

I was wondering if there's already a way to accomplish the same thing using this library?

Essentially I'm hoping to preserve this behavior of taking an example schema:

{
   "paths": {
     "/awesome/endpoint": {
       "post": {
          "responses": {
            "400": {
              "$ref": "#/components/responses/400"
            }
          }
       }
     }
   },
   "components": {
     "responses": {
       "400": {
         "$ref": "https://example.com/path/to/my/oas/file.oas3.json#/components/responses/400"
     }
  }
}

and only dereference the remote HTTP reference:

{
   "paths": {
     "/awesome/endpoint": {
       "post": {
          "responses": {
            "400": {
              "$ref": "#/components/responses/400"
            }
          }
       }
     }
   },
   "components": {
     "responses": {
       "400": {
         "description": "The request syntax was malformed and could not be processed.",
          "content": {
             "application/json": {
                "type": "object",
                "properties": {
                  "statusCode": {
                    "type": "integer"
                  },
                  "message": {
                    "type": "string"
                  }
                }
             }
          }
       }
   }
}

@P0lip sorry to ping you directly, but I noticed that you've been active in both projects. If you have any pointers it would be much appreciated.

Hopefully this summary makes sense. Thanks!

@denver-HJS
Copy link
Author

@philsturgeon sorry to mention you directly too, but I see in one of the issues in the now deprecated Stoplight project referenced above that you commented that you've been able to meet your requirements with this library. I'm wondering if you have any suggestions for using it to only dereference the remote references and leave the inline ones in place?

@philsturgeon
Copy link
Member

Why not use bundle if you're ok with references but want them to exist as local refs instead? That's exactly what it's for. :)

@denver-HJS
Copy link
Author

Hmmm, I thought I tried this but I might have been dereferencing & bundling in some incorrect combination. I'll give that another look. Thanks!

@agavazov
Copy link

agavazov commented May 21, 2021

I have the same issue, but I'm not able to solve it with bundle.
Here my example:
oas-root.yaml

paths:
  /example1:
    get:
      $ref: "./oas-example1.yaml"
  /example2:
    get:
      $ref: "#/components/schemas/Error"
components:
  schemas:
    Error: "...."

oas-example1.yaml

ErrorResponse:
  $ref: "#/components/schemas/Error"

Which returns this error: MissingPointerError: Token "components" does not exist.

The only solution I found is to skip JSON Pointer references checks with some extra option like options.resolve.internal at
https://github.com/APIDevTools/json-schema-ref-parser/blob/master/lib/ref.js#L179 or as @denver-HJS mention to have options.resolve.skipInternal

@philsturgeon
Copy link
Member

This is not a bug, it's saying that #/components/schemas/Error does not exist in oas-example1.yaml because, it doesnt! :D

Each ref needs to point to the filepath then # is the start of the pointer. Perhaps you could make a shared model for ErrorResponse.yml then all locations point to that?

https://blog.stoplight.io/keeping-openapi-dry-and-portable

@marc0l92
Copy link

marc0l92 commented Dec 8, 2021

The only method I currently found to ignore the parsing error of local references is to do something like this:

const $RefParser = require("@apidevtools/json-schema-ref-parser")

async function bundle(root) {
    let bundle = {}
    try {
        bundle = await $RefParser.bundle(root, {
            continueOnError: true,
        })
    } catch (e) {
        bundle = e.files.schema
    }
    return bundle
}

const root = './index.yaml'
bundle(root).then(bundle =>{
    console.log(bundle)
})

Apparently even if the bundle function fails the schema is generated ignoring the local references

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

5 participants