-
Notifications
You must be signed in to change notification settings - Fork 9.2k
fix(config): cast configuration values into proper types #9829
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b31de5e
fix(config): cast configuration values into proper types
glowcloud 5062e48
fix: fix empty string filter not rendering field
glowcloud 2dc93a1
change to check type of maxDisplayedTags
glowcloud d717544
apply requested changes
glowcloud cc23a87
update test mocks
glowcloud eb399cf
update default options and getting them
glowcloud b847ba4
remove null filter test
glowcloud 42bd461
update type casters, default values and factorization
glowcloud 2cc6cbb
Merge branch 'master' into issue-9808
glowcloud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,13 @@ | |
| * | ||
| * NOTE1: lodash.merge & lodash.mergeWith prefers to ignore undefined values | ||
| * NOTE2: special handling of `domNode` option is now required as `deep-extend` will corrupt it (lodash.merge handles it correctly) | ||
| * NOTE3: oauth2RedirectUrl and withCredentials options can be set to undefined. By expecting null instead of undefined, we can't use lodash.merge. | ||
| * NOTE3: oauth2RedirectUrl option can be set to undefined. By expecting null instead of undefined, we can't use lodash.merge. | ||
| * NOTE4: urls.primaryName needs to handled in special way, because it's an arbitrary property on Array instance | ||
| * | ||
| * TODO([email protected]): remove deep-extend in favor of lodash.merge | ||
| */ | ||
| import deepExtend from "deep-extend" | ||
| import typeCast from "./type-cast" | ||
|
|
||
| const merge = (target, ...sources) => { | ||
| let domNode = Symbol.for("domNode") | ||
|
|
@@ -51,7 +52,7 @@ const merge = (target, ...sources) => { | |
| merged.urls.primaryName = primaryName | ||
| } | ||
|
|
||
| return merged | ||
| return typeCast(merged) | ||
| } | ||
|
|
||
| export default merge | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| import has from "lodash/has" | ||
| import get from "lodash/get" | ||
| import set from "lodash/fp/set" | ||
|
|
||
| import typeCasters from "./mappings" | ||
|
|
||
| const typeCast = (options) => { | ||
char0n marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Object.entries(typeCasters).reduce( | ||
| (acc, [optionPath, { typeCaster, defaultValue }]) => { | ||
| if (has(acc, optionPath)) { | ||
| const uncasted = get(acc, optionPath) | ||
| const casted = typeCaster(uncasted, defaultValue) | ||
| acc = set(optionPath, casted, acc) | ||
| } | ||
| return acc | ||
| }, | ||
| { ...options } | ||
| ) | ||
| } | ||
|
|
||
| export default typeCast | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| import arrayTypeCaster from "./type-casters/array" | ||
| import booleanTypeCaster from "./type-casters/boolean" | ||
| import domNodeTypeCaster from "./type-casters/dom-node" | ||
| import filterTypeCaster from "./type-casters/filter" | ||
| import nullableArrayTypeCaster from "./type-casters/nullable-array" | ||
| import nullableStringTypeCaster from "./type-casters/nullable-string" | ||
| import numberTypeCaster from "./type-casters/number" | ||
| import objectTypeCaster from "./type-casters/object" | ||
| import stringTypeCaster from "./type-casters/string" | ||
| import syntaxHighlightTypeCaster from "./type-casters/syntax-highlight" | ||
| import undefinedStringTypeCaster from "./type-casters/undefined-string" | ||
| import defaultOptions from "../defaults" | ||
|
|
||
| const typeCasters = { | ||
| configUrl: { typeCaster: stringTypeCaster }, | ||
| deepLinking: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.deepLinking, | ||
| }, | ||
| defaultModelExpandDepth: { | ||
| typeCaster: numberTypeCaster, | ||
| defaultValue: defaultOptions.defaultModelExpandDepth, | ||
| }, | ||
| defaultModelRendering: { typeCaster: stringTypeCaster }, | ||
| defaultModelsExpandDepth: { | ||
| typeCaster: numberTypeCaster, | ||
| defaultValue: defaultOptions.defaultModelsExpandDepth, | ||
| }, | ||
| displayOperationId: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.displayOperationId, | ||
| }, | ||
| displayRequestDuration: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.displayRequestDuration, | ||
| }, | ||
| docExpansion: { typeCaster: stringTypeCaster }, | ||
| dom_id: { typeCaster: nullableStringTypeCaster }, | ||
| domNode: { typeCaster: domNodeTypeCaster }, | ||
| filter: { typeCaster: filterTypeCaster }, | ||
| layout: { typeCaster: stringTypeCaster }, | ||
| maxDisplayedTags: { | ||
| typeCaster: numberTypeCaster, | ||
| defaultValue: defaultOptions.maxDisplayedTags, | ||
| }, | ||
| oauth2RedirectUrl: { typeCaster: undefinedStringTypeCaster }, | ||
| persistAuthorization: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.persistAuthorization, | ||
| }, | ||
| plugins: { | ||
| typeCaster: arrayTypeCaster, | ||
| defaultValue: defaultOptions.plugins, | ||
| }, | ||
| pluginsOptions: { | ||
| typeCaster: objectTypeCaster, | ||
| pluginsOptions: defaultOptions.pluginsOptions, | ||
| }, | ||
| "pluginsOptions.pluginsLoadType": { typeCaster: stringTypeCaster }, | ||
| presets: { | ||
| typeCaster: arrayTypeCaster, | ||
| defaultValue: defaultOptions.presets, | ||
| }, | ||
| requestSnippets: { | ||
| typeCaster: objectTypeCaster, | ||
| defaultValue: defaultOptions.requestSnippets, | ||
| }, | ||
| requestSnippetsEnabled: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.requestSnippetsEnabled, | ||
| }, | ||
| showCommonExtensions: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.showCommonExtensions, | ||
| }, | ||
| showExtensions: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.showExtensions, | ||
| }, | ||
| showMutatedRequest: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.showMutatedRequest, | ||
| }, | ||
| spec: { typeCaster: objectTypeCaster, defaultValue: defaultOptions.spec }, | ||
| supportedSubmitMethods: { | ||
| typeCaster: arrayTypeCaster, | ||
| defaultValue: defaultOptions.supportedSubmitMethods, | ||
| }, | ||
| syntaxHighlight: { | ||
| typeCaster: syntaxHighlightTypeCaster, | ||
| defaultValue: defaultOptions.syntaxHighlight, | ||
| }, | ||
| "syntaxHighlight.activated": { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.syntaxHighlight.activated, | ||
| }, | ||
| "syntaxHighlight.theme": { typeCaster: stringTypeCaster }, | ||
| tryItOutEnabled: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.tryItOutEnabled, | ||
| }, | ||
| url: { typeCaster: stringTypeCaster }, | ||
| urls: { typeCaster: nullableArrayTypeCaster }, | ||
| "urls.primaryName": { typeCaster: stringTypeCaster }, | ||
| validatorUrl: { typeCaster: nullableStringTypeCaster }, | ||
| withCredentials: { | ||
| typeCaster: booleanTypeCaster, | ||
| defaultValue: defaultOptions.withCredentials, | ||
| }, | ||
| } | ||
|
|
||
| export default typeCasters |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const arrayTypeCaster = (value, defaultValue = []) => | ||
| Array.isArray(value) ? value : defaultValue | ||
|
|
||
| export default arrayTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const booleanTypeCaster = (value, defaultValue = false) => | ||
| value === true || value === "true" || value === 1 || value === "1" | ||
| ? true | ||
| : value === false || value === "false" || value === 0 || value === "0" | ||
| ? false | ||
| : defaultValue | ||
|
|
||
| export default booleanTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const domNodeTypeCaster = (value) => | ||
| value === null || value === "null" ? null : value | ||
char0n marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export default domNodeTypeCaster | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| import booleanTypeCaster from "./boolean" | ||
|
|
||
| const filterTypeCaster = (value) => { | ||
| const defaultValue = String(value) | ||
| return booleanTypeCaster(value, defaultValue) | ||
| } | ||
|
|
||
| export default filterTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const nullableArrayTypeCaster = (value) => (Array.isArray(value) ? value : null) | ||
|
|
||
| export default nullableArrayTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const nullableStringTypeCaster = (value) => | ||
| value === null || value === "null" ? null : String(value) | ||
|
|
||
| export default nullableStringTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const numberTypeCaster = (value, defaultValue = -1) => { | ||
| const parsedValue = parseInt(value, 10) | ||
| return Number.isNaN(parsedValue) ? defaultValue : parsedValue | ||
| } | ||
|
|
||
| export default numberTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| import isPlainObject from "lodash/isPlainObject" | ||
|
|
||
| const objectTypeCaster = (value, defaultValue = {}) => | ||
| isPlainObject(value) ? value : defaultValue | ||
|
|
||
| export default objectTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const stringTypeCaster = (value) => String(value) | ||
|
|
||
| export default stringTypeCaster |
14 changes: 14 additions & 0 deletions
14
src/core/config/type-cast/type-casters/syntax-highlight.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| import isPlainObject from "lodash/isPlainObject" | ||
|
|
||
| const syntaxHighlightTypeCaster = (value, defaultValue) => { | ||
| return isPlainObject(value) | ||
| ? value | ||
| : value === false || value === "false" || value === 0 || value === "0" | ||
| ? { activated: false } | ||
| : defaultValue | ||
| } | ||
|
|
||
| export default syntaxHighlightTypeCaster |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| const undefinedStringTypeCaster = (value) => | ||
| value === undefined || value === "undefined" ? undefined : String(value) | ||
|
|
||
| export default undefinedStringTypeCaster |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.