-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add possibility to hook into the config initialization. #2590
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -229,6 +229,7 @@ The steps described above will get you up and running with minimal setup. Howeve | |||||||||||||
* [Customize index.html](#customize-indexhtml) | ||||||||||||||
* [Enable OAuth2.0 Flows](#enable-oauth20-flows) | ||||||||||||||
* [Use client-side request and response interceptors](#use-client-side-request-and-response-interceptors) | ||||||||||||||
* [Change Swagger UI Config](#change-swagger-ui-config) | ||||||||||||||
|
||||||||||||||
* [Swashbuckle.AspNetCore.Annotations](#swashbuckleaspnetcoreannotations) | ||||||||||||||
* [Install and Enable Annotations](#install-and-enable-annotations) | ||||||||||||||
|
@@ -1265,7 +1266,7 @@ app.UseSwaggerUI(c => | |||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
_NOTE: The `InjectOnCompleteJavaScript` and `InjectOnFailureJavaScript` options have been removed because the latest version of swagger-ui doesn't expose the necessary hooks. Instead, it provides a [flexible customization system](https://github.com/swagger-api/swagger-ui/blob/master/docs/customization/overview.md) based on concepts and patterns from React and Redux. To leverage this, you'll need to provide a custom version of index.html as described [below](#customize-indexhtml)._ | ||||||||||||||
_NOTE: The `InjectOnCompleteJavaScript` and `InjectOnFailureJavaScript` options have been removed because the latest version of swagger-ui doesn't expose the necessary hooks. Instead, it provides a [flexible customization system](https://github.com/swagger-api/swagger-ui/blob/master/docs/customization/overview.md) based on concepts and patterns from React and Redux. To leverage this, adjust the [configuration initialization with plugins](#change-swagger-ui-config) or you'll need to provide a custom version of index.html as described [below](#customize-indexhtml)._ | ||||||||||||||
|
||||||||||||||
The [custom index sample app](test/WebSites/CustomUIIndex/Swagger/index.html) demonstrates this approach, using the swagger-ui plugin system provide a custom topbar, and to hide the info component. | ||||||||||||||
|
||||||||||||||
|
@@ -1341,6 +1342,78 @@ app.UseSwaggerUI(c => | |||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
### Change Swagger UI Config ### | ||||||||||||||
|
||||||||||||||
Swagger UI has a big variety of options and a mighty plugin API allowing customizations. | ||||||||||||||
To customize the Swagger UI (and OAuth) configuration objects before they are passed into the Swagger UI initialization, | ||||||||||||||
inject a custom JavaScript file which defines a global function with the following signature. | ||||||||||||||
In many cases this feature allows to avoid using a custom `index.html` in your project. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
/** | ||||||||||||||
* @param configObject See https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ | ||||||||||||||
* @param oauthConfigObject See https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/ | ||||||||||||||
*/ | ||||||||||||||
function initConfig(configObject, oauthConfigObject) { } | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
Through this mechanism any options that Swagger UI supports can be set and also plugins can be registered. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
Swagger UI is written with React so it can be a bit tricky to write custom components. | ||||||||||||||
Here a full example which adds a custom section to the parameters of all operations: | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
// swagger-ui.js | ||||||||||||||
// https://swagger.io/docs/open-source-tools/swagger-ui/customization/plugin-api/ | ||||||||||||||
function configInit(config) { | ||||||||||||||
if(!config.plugins) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
config.plugins = []; | ||||||||||||||
} | ||||||||||||||
config.plugins.push(TopbarPlugin); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const TopbarPlugin = (system) => { | ||||||||||||||
return { | ||||||||||||||
// Register an own React component | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
components: { | ||||||||||||||
CustomOperationDetails: class CustomOperationDetails extends system.React.Component { | ||||||||||||||
render() { | ||||||||||||||
const hello = this.props?.operation?.get('x-hello'); | ||||||||||||||
if(!hello) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
return ''; | ||||||||||||||
} | ||||||||||||||
return system.React.createElement('div', | ||||||||||||||
{ className: 'hello' }, | ||||||||||||||
hello | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
}, | ||||||||||||||
wrapComponents: { | ||||||||||||||
// wrap the parameters component | ||||||||||||||
parameters: Original => (props => { | ||||||||||||||
// get the custom React component | ||||||||||||||
Comment on lines
+1392
to
+1394
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
const CustomOperationDetails = props.getComponent('CustomOperationDetails'); | ||||||||||||||
// Use the non JSX React API (https://reactjs.org/docs/react-without-jsx.html) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
// to create a wrapper div holding our own component and the original parameters component | ||||||||||||||
return system.React.createElement('div', | ||||||||||||||
{ className: "parameters-wrap"}, | ||||||||||||||
system.React.createElement(CustomOperationDetails, props), | ||||||||||||||
system.React.createElement(Original, props) | ||||||||||||||
); | ||||||||||||||
Comment on lines
+1398
to
+1402
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example looks out-of-date compared to the example in the non-obsolete React documentation. |
||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
```csharp | ||||||||||||||
app.UseSwaggerUI(options = > | ||||||||||||||
{ | ||||||||||||||
options.InjectJavascript("swagger-ui.js"); | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
Comment on lines
+1410
to
+1416
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
## Swashbuckle.AspNetCore.Annotations ## | ||||||||||||||
|
||||||||||||||
### Install and Enable Annotations ### | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a sample and/or integration test that can be updated to demonstrate this somehow? |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -102,6 +102,11 @@ | |||||
if (interceptors.ResponseInterceptorFunction) | ||||||
configObject.responseInterceptor = parseFunction(interceptors.ResponseInterceptorFunction); | ||||||
|
||||||
// Allow adjusting config through injected JavaScript files | ||||||
if(typeof initConfig === "function") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better way of hooking this up as a global that's more robust and/or more likely to not pollute with any existing objects (e.g. what if it was called |
||||||
initConfig(configObject, oauthConfigObject); | ||||||
} | ||||||
|
||||||
// Begin Swagger UI call region | ||||||
|
||||||
const ui = SwaggerUIBundle(configObject); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.