openapi-sampler icon indicating copy to clipboard operation
openapi-sampler copied to clipboard

Invalid reference token and decoding

Open HonzaMac opened this issue 2 years ago • 1 comments

We are receiving error below, when trying to generate documentation via redoc-cli tool. redoc-cli calls openapi-sampler and does not work properly with keys in swagger.

Example of swagger file.

``` components: schemas: Required__%5BkeyinDeliverableWithWriterAlias%5D%3Aboolean--__: properties: system-administration: type: boolean system-use: type: boolean required: - system-administration - system-use type: object description: 'Make all properties in T required' ReleaseNotificationsBody: properties: commentsAll: type: boolean comments: $ref: '#/components/schemas/Required__%5BkeyinDeliverableWithWriterAlias%5D%3Aboolean--__' required: - commentsAll - comments type: object additionalProperties: false info: title: 'API v1' version: 3.18.0 openapi: 3.0.0 paths: '/releases/{releaseVersionId}/notifications': put: operationId: updateNotifications responses: '200': description: 'Release notifications updated' content: application/json: schema: $ref: '#/components/schemas/ReleaseNotificationsBody' summary: 'Update Release Notifications for current user' parameters: - in: path name: releaseVersionId required: true schema: $ref: '#/components/schemas/ReleaseVersionId' requestBody: required: true content: application/json: schema: $ref: '#/components/schemas/ReleaseNotificationsBody' get: operationId: getNotifications responses: '200': description: 'Get release notifications' content: application/json: schema: $ref: '#/components/schemas/ReleaseNotificationsBody' summary: 'Get Release Notifications for current user' parameters: - in: path name: releaseVersionId required: true schema: $ref: '#/components/schemas/ReleaseVersionId' ```

Error: Invalid reference token: Required__[keyinDeliverableWithWriterAlias]:boolean--__
at Function.get (/sr/app/node modules/openapi-sampler/dist/openapi-sampler.js:79:19)
at traverse (/usr/app/node_modules/openapi-sampler/dist/openapi-sampler.js:947:43)
at /usr/app/node_modules/openapi-sampler/dist/openapi-sampler.is:646:43
at Arrav.forEach(<anonvmous>)
at sample0biect (/us/app/node modules/openapi-sampler/dist/openapi-sampler.is:640:36
at traverse (/usr/app/node_modules/openapi-sampler/dist/openapi-sampler.js:1033:17)
at traverse (/usr/app/node_modules/openapi-sampler/dist/openapi-sampler.is:953:16)
at Object.sample (/usr/app/node_modules/openapi-sampler/dist/openapi-sampler.js:426:33)
at Hn.generateExample (/usr/app/node modules/redoc/bundles/redoc.lib.is:41:73780)
at new Hn (/usr/app/node modules/redoc/bundles/redoc. lib.js:41:73198)

What helped us is this patch:

diff --git a/node_modules/openapi-sampler/dist/openapi-sampler.js b/node_modules/openapi-sampler/dist/openapi-sampler.js
index 55fc164..4431786 100644
--- a/node_modules/openapi-sampler/dist/openapi-sampler.js
+++ b/node_modules/openapi-sampler/dist/openapi-sampler.js
@@ -938,7 +938,7 @@ function traverse(schema, options, spec, context) {
       throw new Error('Your schema contains $ref. You must provide full specification in the third parameter.');
     }

-    var ref = decodeURIComponent(schema.$ref);
+    var ref = schema.$ref; // Fixes: "Error: Invalid reference token: Required__[keyinDeliverableWithWriterAlias]:boolean--__"

     if (ref.startsWith('#')) {
       ref = ref.substring(1);

HonzaMac avatar Feb 01 '23 13:02 HonzaMac

you are using invalid key names for components.

https://spec.openapis.org/oas/v3.0.4.html#fixed-fields-5 https://spec.openapis.org/oas/v3.1.1.html#fixed-fields-5

The spec states:

All the fixed fields declared above are objects that MUST use keys that match the regular expression: ^[a-zA-Z0-9.-_]+$.

The decodeUriComponent method is necessary for resolving other $refs inside the document.

jeremyfiel avatar Apr 13 '25 04:04 jeremyfiel