fix(topbar): handle empty urls array without crashing#10835
Open
yogeshwaran-c wants to merge 1 commit intoswagger-api:masterfrom
Open
fix(topbar): handle empty urls array without crashing#10835yogeshwaran-c wants to merge 1 commit intoswagger-api:masterfrom
yogeshwaran-c wants to merge 1 commit intoswagger-api:masterfrom
Conversation
When the urls configuration option is an empty array, the TopBar render method would crash because it only checked for truthiness of urls (an empty array is truthy) before accessing urls[selectedIndex].url. This adds a length check to match the pattern already used in componentDidMount and setSelectedUrl. Fixes swagger-api#5663
71ca6f2 to
5b01568
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When the
urlsconfiguration option is set to an empty array ([]), the TopBar component crashes because in therender()method it only checksif(urls)before trying to accessurls[this.state.selectedIndex].url. Since an empty array is truthy in JavaScript, this check passes, but then accessing index0of an empty array returnsundefined, and accessing.urlonundefinedthrows a TypeError.The fix adds a
.lengthcheck:if(urls && urls.length), which is consistent with the pattern already used incomponentDidMount()andsetSelectedUrl()within the same file.Motivation and Context
Fixes #5663
Users may configure
urls: []or pass?urls=[]as a query parameter. Instead of crashing, the UI should gracefully fall back to showing the URL text input.How Has This Been Tested?
urlsis[], the component now falls through to theelsebranch and renders the URL text input instead of crashingurlshas entries, behavior is unchangedScreenshots (if appropriate):
N/A
Checklist
My PR contains...
src/is unmodified: changes to documentation, CI, metadata, etc.)package.json)My changes...
Documentation
Automated tests