question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to initialize SwaggerUIBundle in Angular 4 project

See original GitHub issue

Trying to use swagger-ui-dist inside angular4 application. Initialization of SwaggerUIBundle produces an error (promise is overwritten error is thrown from zone.js).

Stacktrace:

swagger-ui-bundle.js:67 Unhandled promise rejection Error: Zone.js has detected that ZoneAwarePromise `(window|global).Promise` has been overwritten.
Most likely cause is that a Promise polyfill has been loaded after Zone.js (Polyfilling Promise api is not necessary when zone.js is loaded. If you must load one, do so before loading zone.js.)
    at Function.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.assertZonePatched (zone.js:44)
    at new NgZone (core.es5.js:3775)
    at PlatformRef_.webpackJsonp.../../../core/@angular/core.es5.js.PlatformRef_._bootstrapModuleFactoryWithZone (core.es5.js:4492)
    at core.es5.js:4537
    at swagger-ui-bundle.js:67
    at swagger-ui-bundle.js:67
    at MutationObserver.f (swagger-ui-bundle.js:67)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.

Please check repository created to reproduce an issue: https://github.com/asakalou/swagger-angular4-issue

Please check swagger component at https://github.com/asakalou/swagger-angular4-issue/blob/master/src/app/swagger/swagger.component.ts

Q A
Bug or feature request? Bug
Which Swagger/OpenAPI version?
Which Swagger-UI version? swagger-ui-dist@3.1.5
How did you install Swagger-UI? via npm
Which broswer & version? Chrome Version 60.0.3112.90 (Official Build) (64-bit)
Which operating system? macOS Sierra 10.12.6

Configuration (browser query string, constructor, config.yaml)

http://petstore.swagger.io/v2/swagger.json

Expected Behavior

No error, should render swagger ui.

Current Behavior

Error during initializing SwaggerUIBundle inside angular4 application.

Context

We are trying to render swagger ui inside our application to provide customers an interface for our API access.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
lauthiebcommented, Aug 25, 2017

Hello @asakalou, I found a solution if you want to integrate swagger-ui on Angular 4. (Thanks to @shockey for your comment)

Firstly, go to polyfills.ts and add this before the import of zone.js :

import {SwaggerUIBundle, SwaggerUIStandalonePreset} from 'swagger-ui-dist';

export const swaggerUIBundle = SwaggerUIBundle;
export const swaggerUIStandalonePreset = SwaggerUIStandalonePreset;`

Then, go to your swagger.component.ts and use the const exported like this :

import { swaggerUIBundle, swaggerUIStandalonePreset } from '../../polyfills';

ngAfterViewInit() {
    const ui = swaggerUIBundle({
      url: 'http://petstore.swagger.io/v2/swagger.json',
      domNode: this.el.nativeElement.querySelector('.swagger-container'),
      deepLinking: true,
      presets: [
        swaggerUIBundle.presets.apis,
        swaggerUIStandalonePreset
      ],
      plugins: [
        swaggerUIBundle.plugins.DownloadUrl
      ],
      layout: 'StandaloneLayout'
    });
}

Finally, add the CSS of Swagger UI in .angular-cli.json :

"styles": [
    "../node_modules/swagger-ui-dist/swagger-ui.css",
    "styles.scss"
]
2reactions
mckennajonescommented, Mar 20, 2018

I would also recommend against using the swagger-ui-dist package in Angular. It causes you bundle sizes to be much larger than they need to be. I was able to use the normal swagger-ui package in an Angular 5 project by doing a simple const SwaggerUI = require("swagger-ui"); in my swagger component. For more info, check out this issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding Swagger UI to Angular App - Stack Overflow
Configure the angular.json file and desired component for swagger-ui. ... OnInit } from '@angular/core'; declare const SwaggerUIBundle: any ...
Read more >
Configuring and Using Swagger UI in ASP.NET Core Web API
By executing these steps, the Swagger is configured and ready for use in our project. Exploring the Swagger UI. First, we are going...
Read more >
Configuration - Swagger Documentation
The swagger-config.yaml in the project root directory, if it exists, is baked into the application; configuration object passed as an argument to Swagger...
Read more >
Swagger UI tutorial | Documenting APIs - Idratherbewriting.com
Or for an easier approach using a visual editor to create the ... Additionally, Swagger UI (which is an actively managed project with...
Read more >
MEAN Stack Tutorial: Create an Example Application | Linode
MEAN is an acronym for the combination of technology stacks–MongoDB, ... ng new my-angular-app --defaults # Initialize Angular project. cd ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found