-
Notifications
You must be signed in to change notification settings - Fork 3
Auto parse JSON string for compatible content type #8
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import { ConfigurationSetting, secretReferenceContentType } from "@azure/app-configuration"; | ||
| import { IKeyValueAdapter } from "./IKeyValueAdapter"; | ||
|
|
||
|
|
||
| export class JsonKeyValueAdapter implements IKeyValueAdapter { | ||
| private static readonly ExcludedJsonContentTypes: string[] = [ | ||
| secretReferenceContentType | ||
| // TODO: exclude application/vnd.microsoft.appconfig.ff+json after feature management is supported | ||
| ]; | ||
|
|
||
| public canProcess(setting: ConfigurationSetting): boolean { | ||
| if (!setting.contentType) { | ||
| return false; | ||
| } | ||
| if (JsonKeyValueAdapter.ExcludedJsonContentTypes.includes(setting.contentType)) { | ||
| return false; | ||
| } | ||
| return isJsonContentType(setting.contentType); | ||
| } | ||
|
|
||
| public async processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]> { | ||
| if (!setting.value) { | ||
| throw new Error("Unexpected empty value for application/json content type."); | ||
| } | ||
| let parsedValue: any; | ||
| try { | ||
| parsedValue = JSON.parse(setting.value); | ||
| } catch (error) { | ||
| parsedValue = setting.value; | ||
| } | ||
| return [setting.key, parsedValue]; | ||
| } | ||
| } | ||
|
|
||
| // Determine whether a content type string is a valid JSON content type. | ||
| // https://docs.microsoft.com/en-us/azure/azure-app-configuration/howto-leverage-json-content-type | ||
| function isJsonContentType(contentTypeValue: string): boolean { | ||
| if (!contentTypeValue) { | ||
| return false; | ||
| } | ||
|
|
||
| let contentTypeNormalized: string = contentTypeValue.trim().toLowerCase(); | ||
| let mimeType: string = contentTypeNormalized.split(";", 1)[0].trim(); | ||
| let typeParts: string[] = mimeType.split("/"); | ||
| if (typeParts.length !== 2) { | ||
| return false; | ||
| } | ||
|
|
||
| if (typeParts[0] !== "application") { | ||
| return false; | ||
| } | ||
|
|
||
| return typeParts[1].split("+").includes("json"); | ||
| } | ||
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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| const chai = require("chai"); | ||
| const chaiAsPromised = require("chai-as-promised"); | ||
| chai.use(chaiAsPromised); | ||
| const expect = chai.expect; | ||
| const { load } = require("../dist/index"); | ||
| const { | ||
| mockAppConfigurationClientListConfigurationSettings, | ||
| restoreMocks, | ||
| createMockedConnectionString, | ||
| createMockedKeyVaultReference | ||
| } = require("./utils/testHelper"); | ||
|
|
||
| const jsonKeyValue = { | ||
| value: '{"Test":{"Level":"Debug"},"Prod":{"Level":"Warning"}}', | ||
| key: 'json.settings.logging', | ||
| label: null, | ||
| contentType: 'application/json', | ||
| lastModified: '2023-05-04T04:32:56.000Z', | ||
| tags: {}, | ||
| etag: 'GdmsLWq3mFjFodVEXUYRmvFr3l_qRiKAW_KdpFbxZKk', | ||
| isReadOnly: false | ||
| }; | ||
| const keyVaultKeyValue = createMockedKeyVaultReference("TestKey", "https://fake-vault-name.vault.azure.net/secrets/fakeSecretName"); | ||
|
|
||
| describe("json", function () { | ||
| beforeEach(() => { | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restoreMocks(); | ||
| }) | ||
|
|
||
| it("should load and parse if content type is application/json", async () => { | ||
| mockAppConfigurationClientListConfigurationSettings([jsonKeyValue]); | ||
|
|
||
| const connectionString = createMockedConnectionString(); | ||
| const settings = await load(connectionString); | ||
| expect(settings).not.undefined; | ||
| const logging = settings.get("json.settings.logging"); | ||
| expect(logging).not.undefined; | ||
| expect(logging.Test).not.undefined; | ||
| expect(logging.Test.Level).eq("Debug"); | ||
| expect(logging.Prod).not.undefined; | ||
| expect(logging.Prod.Level).eq("Warning"); | ||
| }); | ||
|
|
||
| it("should not parse key-vault reference", async () => { | ||
| mockAppConfigurationClientListConfigurationSettings([jsonKeyValue, keyVaultKeyValue]); | ||
|
|
||
| const connectionString = createMockedConnectionString(); | ||
| const settings = await load(connectionString, { | ||
| keyVaultOptions: { | ||
| secretResolver: (url) => `Resolved: ${url.toString()}` | ||
| } | ||
| }); | ||
| expect(settings).not.undefined; | ||
| const resolvedSecret = settings.get("TestKey"); | ||
| expect(resolvedSecret).not.undefined; | ||
| expect(resolvedSecret.uri).undefined; | ||
| expect(typeof resolvedSecret).eq("string"); | ||
| }); | ||
| }) |
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.