-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(config): cast configuration values into proper types (#9829)
Refs #9808
- Loading branch information
Showing
28 changed files
with
359 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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) => { | ||
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 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 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 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 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 | ||
|
||
export default domNodeTypeCaster |
This file contains 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 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 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 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 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 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 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 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 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 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.