bugfix: legacy Docker variables being overridden by default values (via #5006)

* add `onFound` callback to schemas

* add warning to method docs (for #4957)

* implement Docker OAuth2 init block support

* update docs

* add OAUTH_SCOPE_SEPARATOR

* drop OAuth env from Dockerfile and run script

* don't indent the first oauth block line

* drop unused `dedent` import

* touch up warning message

* add more test cases

* return an empty block if no OAuth content is generated

* fix broken doc line

* allow legacy values to override base values
This commit is contained in:
kyle
2018-11-06 01:57:20 +00:00
committed by GitHub
parent 30f3f10bf5
commit 02758b8125
2 changed files with 99 additions and 9 deletions

View File

@@ -1,46 +1,52 @@
// Converts an object of environment variables into a Swagger UI config object // Converts an object of environment variables into a Swagger UI config object
const configSchema = require("./variables") const configSchema = require("./variables")
const baseConfig = { const defaultBaseConfig = {
url: { url: {
value: "https://petstore.swagger.io/v2/swagger.json", value: "https://petstore.swagger.io/v2/swagger.json",
schema: { schema: {
type: "string" type: "string",
base: true
} }
}, },
dom_id: { dom_id: {
value: "#swagger-ui", value: "#swagger-ui",
schema: { schema: {
type: "string" type: "string",
base: true
} }
}, },
deepLinking: { deepLinking: {
value: "true", value: "true",
schema: { schema: {
type: "boolean" type: "boolean",
base: true
} }
}, },
presets: { presets: {
value: `[\n SwaggerUIBundle.presets.apis,\n SwaggerUIStandalonePreset\n]`, value: `[\n SwaggerUIBundle.presets.apis,\n SwaggerUIStandalonePreset\n]`,
schema: { schema: {
type: "array" type: "array",
base: true
} }
}, },
plugins: { plugins: {
value: `[\n SwaggerUIBundle.plugins.DownloadUrl\n]`, value: `[\n SwaggerUIBundle.plugins.DownloadUrl\n]`,
schema: { schema: {
type: "array" type: "array",
base: true
} }
}, },
layout: { layout: {
value: "StandaloneLayout", value: "StandaloneLayout",
schema: { schema: {
type: "string" type: "string",
base: true
} }
} }
} }
function objectToKeyValueString(env, { injectBaseConfig = false, schema = configSchema } = {}) { function objectToKeyValueString(env, { injectBaseConfig = false, schema = configSchema, baseConfig = defaultBaseConfig } = {}) {
let valueStorage = injectBaseConfig ? Object.assign({}, baseConfig) : {} let valueStorage = injectBaseConfig ? Object.assign({}, baseConfig) : {}
const keys = Object.keys(env) const keys = Object.keys(env)
@@ -62,8 +68,9 @@ function objectToKeyValueString(env, { injectBaseConfig = false, schema = config
const storageContents = valueStorage[varSchema.name] const storageContents = valueStorage[varSchema.name]
if(storageContents) { if(storageContents) {
if(varSchema.legacy === true) { if (varSchema.legacy === true && !storageContents.schema.base) {
// If we're looking at a legacy var, it should lose out to any already-set value // If we're looking at a legacy var, it should lose out to any already-set value
// except for base values
return return
} }
delete valueStorage[varSchema.name] delete valueStorage[varSchema.name]

View File

@@ -32,6 +32,65 @@ describe("docker: env translator", function() {
expect(onFoundSpy.calls.length).toEqual(1) expect(onFoundSpy.calls.length).toEqual(1)
}) })
it("should use a regular value over a legacy one, regardless of order", function () {
const schema = {
MY_THING: {
type: "string",
name: "myThing"
},
MY_OTHER_THING: {
type: "string",
name: "myThing",
legacy: true
}
}
// Regular value provided first
expect(translator({
MY_THING: "hey",
MY_OTHER_THING: "hello"
}, {
schema
})).toEqual(`myThing: "hey",`)
// Legacy value provided first
expect(translator({
MY_OTHER_THING: "hello",
MY_THING: "hey"
}, {
schema
})).toEqual(`myThing: "hey",`)
})
it("should use a legacy value over a base one, regardless of order", function () {
const schema = {
MY_THING: {
type: "string",
name: "myThing",
legacy: true
}
}
const baseConfig = {
myThing: {
value: "base",
schema: {
type: "string",
base: true
}
}
}
// Regular value provided first
expect(translator({
MY_THING: "legacy"
}, {
injectBaseConfig: true,
schema,
baseConfig
})).toEqual(`myThing: "legacy",`)
})
}) })
describe("Swagger UI configuration", function() { describe("Swagger UI configuration", function() {
it("should generate a base config including the base content", function () { it("should generate a base config including the base content", function () {
@@ -138,6 +197,30 @@ describe("docker: env translator", function() {
).trim()) ).trim())
}) })
it("should pick up legacy variables when using base config", function () {
const input = {
API_URL: "/swagger.json",
API_URLS: `["/one", "/two"]`,
}
expect(translator(input, { injectBaseConfig: true })).toEqual(dedent(`
"dom_id": "#swagger-ui",
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
url: "/swagger.json",
urls: ["/one", "/two"],`
).trim())
})
it("should generate a full config k:v string", function () { it("should generate a full config k:v string", function () {
const input = { const input = {
API_URL: "/old.yaml", API_URL: "/old.yaml",