swagger-ui: Unable to initialize SwaggerUIBundle in Angular 4 project
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (4 by maintainers)
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 :
Then, go to your swagger.component.ts and use the const exported like this :
Finally, add the CSS of Swagger UI in .angular-cli.json :
I would also recommend against using the
swagger-ui-distpackage in Angular. It causes you bundle sizes to be much larger than they need to be. I was able to use the normalswagger-uipackage in an Angular 5 project by doing a simpleconst SwaggerUI = require("swagger-ui");in my swagger component. For more info, check out this issueHey @Brentei,
My solution to the CSS issue was to use angular’s View Encapsulation feature. Most browsers don’t yet support Native encapsulation, but Emulated worked OK for me in this scenario.
So in my Swagger component’s CSS file I imported the swagger CSS:
@import "~swagger-ui/dist/swagger-ui.cssAnd in my component that uses bootstrap:
@import "~bootstrap/dist/css/bootstrap.min.css";This obviously isn’t a great solution if you want to use bootstrap in multiple components, but it worked for me.
Got this working in my angular app, but the CSS isn’t quite working properly as you can see in this screenshot.
As you can see everything is right justified, the authorize lock is missing, and the carrot for expanding the section is also missing. Any ideas @lauthieb or @asakalou ? Did you run into this issue?
Thanks
Hi everyone, I just fixed this in a PR.
Basically, I changed our internal polyfill to only take effect when there’s no Promise implementation at all (see the diff here).
It’s a naive solution, but it should work well for this use case. The fix should go live tonight when we do our weekly release.
Thanks @asakalou & @lauthieb! Please let me know if there are any lingering issues with this.