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

Version 6 doesn't correctly generate swagger-ui-init.js #1962

Closed
2 of 4 tasks
Panzer1119 opened this issue Jul 8, 2022 · 0 comments · Fixed by #1963
Closed
2 of 4 tasks

Version 6 doesn't correctly generate swagger-ui-init.js #1962

Panzer1119 opened this issue Jul 8, 2022 · 0 comments · Fixed by #1963

Comments

@Panzer1119
Copy link
Contributor

Panzer1119 commented Jul 8, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The generated swagger-ui-init.js does not have functions correctly replaced in the swagger options.

For example the previous version generated something like this:

// [...]
  var options = {
  "swaggerDoc": {
    "openapi": "3.0.0",
    "paths": {
      // [...]
    },
    "info": {
      // [...]},
    "tags": [],
    "servers": [],
    "components": {
      "schemas": {}
    }
  },
  "customOptions": {
    "operationsSorter": (a, b) => a.get("id").localeCompare(b.get("id")) // Type of function is kept
  },
  "swaggerUrl": {}
};
// [...]

But the new version generates something like this:

// [...]
  let options = {
  "swaggerDoc": {
    "openapi": "3.0.0",
    "paths": {
      // [...]
    },
    "info": {
      // [...]
    },
    "tags": [],
    "servers": [],
    "components": {
      "schemas": {}
    }
  },
  "customOptions": {
    "operationsSorter": "(a, b) => a.get("id").localeCompare(b.get("id"))" // Almost the same, but has " before and after the function, which cause an Error
  }
};
// [...]

This leads to an empty page when trying to access the generated Swagger API Documention.

Minimum reproduction code

https://github.com/Panzer1119/nestjs-swagger-build-jsinit-options

Steps to reproduce

  1. npm i
  2. npm start
  3. Open the Swagger API Documentation in Browser (URL is printed to console)
  4. Inspect the Sources and look at api/swagger-ui-init.js

Expected behavior

Since Version 6 this module provides it's own implementations for the swagger HTML generation etc.

But the newly created lib/swagger-ui/helpers.ts, which basically contains some code from swagger-ui-express index.js, fails to correctly replace the function placeholder, because it misses the " before and after the placeholder in the RegExp.

This can be fixed by adding them back and then the built JSInitOptions will have functions as functions and not strings set.

Original Code (Line 21):

json = json.replace(new RegExp(functionPlaceholder, 'g'), () => fns.shift());

Fixed Code:

json = json.replace(new RegExp('"' + functionPlaceholder + '"', 'g'), () => fns.shift());

Package version

9.0.1

NestJS version

9.0.1

Node.js version

16.13.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Panzer1119 added a commit to Panzer1119/nestjs-swagger that referenced this issue Jul 8, 2022
Panzer1119 added a commit to Panzer1119/nestjs-swagger that referenced this issue Jul 8, 2022
Panzer1119 added a commit to Panzer1119/nestjs-swagger that referenced this issue Jul 8, 2022
When building JS InitOptions, functions get replaced by a placeholder.
When stringified quotes are added, so we need to replace the placeholder
including the quotes.

Fixes:  nestjs#1962
Panzer1119 added a commit to Panzer1119/nestjs-swagger that referenced this issue Jul 8, 2022
When building JS InitOptions, functions get replaced by a placeholder.
JSON.stringify quotes strings, so we need to replace the placeholder
including the quotes.

Fixes:  nestjs#1962
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant