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.

Integrating an own component for ApiKeyAuth via Plugin API doesn't work

See original GitHub issue
Q A
Bug or feature request? Bug (probably)
Which Swagger/OpenAPI version? 2.0
Which Swagger-UI version? 3.13.4
How did you install Swagger-UI? via npm/webpack
Which browser & version? Safari 11.1
Which operating system? MacOS 10.13.4

In our project we use the plugin api to override several components, such as InfoUrl and InfoBasePath which works like expected. If we try the same for the component ApiKeyAuth it doesn’t have any impact to our UI, even if we cancel it out completely:

export const ApiKeyAuthPlugin = () => {
	return {
		components: {
			ApiKeyAuth: () => null
		}
	}
}

Demonstration API definition

"security": [
	{
		"api_key": []
	}
],
"securityDefinitions": {
	"api_key": {
		"in": "header",
		"name": "x-api-key",
		"type": "apiKey",
	}
},

Expected Behavior

With the above described plugin, the component should be cleared out which should result in an empty modal in our UI.

Current Behavior

Nothing happens. The UI looks like without a plugin at all.

Context

We need to set a default apiKey for our tryout requests from swagger-ui. So the users can decide whether they want to use the default one or their own. Therefore, we want to display the apiKey as a default in the authorization modal. Additionally this apiKey should even be set as default before the user clicks authorize. As this is not possible with the current swagger-ui, we want to create our own plugin to overwrite the apiKeyAuth component.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
shockeycommented, May 8, 2018

Hi @ChristophWalter!

If we try the same for the component ApiKeyAuth it doesn’t have any impact to our UI, even if we cancel it out completely:

Try using apiKeyAuth instead of ApiKeyAuth 😄 component names are case-sensitive, and not 100% consistent across the project…

To quote myself in #4431:

For context: #3393 is open, to make component names case-insensitive. There was a PR for the issue (#3419), which stored all components internally as lower-case, and then downcased all component lookups - but the contributor withdrew the code 😢

For future reference, you can use the getComponents method to see an object with every component’s name as a key, though in a perfect world you wouldn’t have to care about the case.


Don’t know how to access values within the swaggerfile during initialization, yet.

You can do this from within an onComplete, like so:

const ui = SwaggerUI({
  url: "http://petstore.swagger.io/v2/swagger.json",
  onComplete: () => {
    const spec = ui.specSelectors.specJson() // this is an Immutable.js Map
    const tokenValue = spec.getIn(["securityDefinitions", "MyAuth", "x-default"])

    ui.preauthorizeApiKey("MyAuth", tokenValue)
  }
})
1reaction
shockeycommented, May 10, 2018

@ChristophWalter no problem! Immutable has a very powerful API, for this particular thing you can use ui.specSelectors.specJson().getIn(["a", "deep", "path"]) to grab a value deeply within your data. Or you could always use ui.specSelectors.specJson().toJS() if you want a plain JS object 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connecting Make to any web service that uses API tokens ...
Create an application on the web service's website. Obtain the API Key/API token. Add Make's HTTP > Make an API Key Auth request ......
Read more >
Authentication — API Key Auth - Zapier Platform UI
To add API Key Auth to your Zapier integration, open the Authentication tab in Zapier visual builder and select API key. You then...
Read more >
Splitwise API
This documentation will help you to fetch information on users, expenses, groups, and much more. If something in the API is confusing you,...
Read more >
Add Components to Flex UI - Twilio
Add Components to Flex UI. In this guide, we'll add a custom component to the TaskInfoPanel that features a simple to-do list for...
Read more >
Using OpenAPI and Swagger UI - Quarkus
This guide explains how your Quarkus application can expose its API description through an OpenAPI specification and how you can test it via...
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