Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@azure/app-configuration": "^1.6.1",
"@azure/identity": "^4.2.1",
"@azure/keyvault-secrets": "^4.7.0"
"@azure/keyvault-secrets": "^4.7.0",
"jsonc-parser": "^3.3.1"
}
}
1 change: 1 addition & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default [
"@azure/identity",
"crypto",
"dns/promises",
"jsonc-parser",
"@microsoft/feature-management"
],
input: "src/index.ts",
Expand Down
14 changes: 12 additions & 2 deletions src/JsonKeyValueAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { ConfigurationSetting, featureFlagContentType, secretReferenceContentType } from "@azure/app-configuration";
import { stripComments } from "jsonc-parser";
import { parseContentType, isJsonContentType } from "./common/contentType.js";
import { IKeyValueAdapter } from "./IKeyValueAdapter.js";

Expand All @@ -26,9 +27,18 @@ export class JsonKeyValueAdapter implements IKeyValueAdapter {
let parsedValue: unknown;
if (setting.value !== undefined) {
try {
parsedValue = JSON.parse(setting.value);
let cleanJsonValue = setting.value;
if (setting.value) {
cleanJsonValue = stripComments(setting.value);
}
parsedValue = JSON.parse(cleanJsonValue);
} catch (error) {
parsedValue = setting.value;
if (error instanceof SyntaxError) {
parsedValue = setting.value;
} else {
// If the error is not a SyntaxError, rethrow it
throw error;
}
}
} else {
parsedValue = setting.value;
Expand Down
128 changes: 127 additions & 1 deletion test/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("json", function () {
createMockedJsonKeyValue("json.settings.object", "{}"),
createMockedJsonKeyValue("json.settings.array", "[]"),
createMockedJsonKeyValue("json.settings.number", "8"),
createMockedJsonKeyValue("json.settings.string", "string"),
createMockedJsonKeyValue("json.settings.string", "\"string\""),
createMockedJsonKeyValue("json.settings.false", "false"),
createMockedJsonKeyValue("json.settings.true", "true"),
createMockedJsonKeyValue("json.settings.null", "null"),
Expand Down Expand Up @@ -88,4 +88,130 @@ describe("json", function () {
expect(settings.get("json.settings.emptyString")).eq("", "is empty string");
expect(settings.get("json.settings.illegalString")).eq("[unclosed", "is illegal string");
});

it("should parse json with single-line comments", async () => {
const jsoncValue = `{
// This is a single-line comment
"database": {
"host": "localhost", // Another comment
"port": 5432
},
"debug": true
}`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.withComments", jsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);

const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get<any>("jsonc.settings.withComments");
expect(config).not.undefined;
expect(config.database).not.undefined;
expect(config.database.host).eq("localhost");
expect(config.database.port).eq(5432);
expect(config.debug).eq(true);
});

it("should parse json with multi-line comments", async () => {
const jsoncValue = `{
/*
* This is a multi-line comment
* describing the configuration
*/
"app": {
"name": "TestApp",
/* inline multi-line comment */ "version": "1.0.0"
},
/*
"disabled": "this entire section is commented out"
*/
"enabled": true
}`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.multilineComments", jsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);

const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get<any>("jsonc.settings.multilineComments");
expect(config).not.undefined;
expect(config.app).not.undefined;
expect(config.app.name).eq("TestApp");
expect(config.app.version).eq("1.0.0");
expect(config.enabled).eq(true);
expect(config.disabled).undefined; // Should be undefined as it's commented out
});

it("should parse json with mixed comment types", async () => {
const jsoncValue = `{
// Configuration for the application
"application": {
"name": "Azure App Config Test", // Application name
"version": "2.0.0",
/*
* Environment settings
* These can be overridden per environment
*/
"environment": {
"development": {
"debug": true,
"logLevel": "debug"
},
"production": {
"debug": false,
"logLevel": "error"
}
}
},
// Features configuration
"features": [
"authentication",
"logging",
/* "experimental-feature", */ // Commented out feature
"monitoring"
]
}`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.complex", jsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);

const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get<any>("jsonc.settings.complex");
expect(config).not.undefined;
expect(config.application).not.undefined;
expect(config.application.name).eq("Azure App Config Test");
expect(config.application.version).eq("2.0.0");
expect(config.application.environment).not.undefined;
expect(config.application.environment.development).not.undefined;
expect(config.application.environment.development.debug).eq(true);
expect(config.application.environment.development.logLevel).eq("debug");
expect(config.application.environment.production).not.undefined;
expect(config.application.environment.production.debug).eq(false);
expect(config.application.environment.production.logLevel).eq("error");
expect(config.features).not.undefined;
expect(Array.isArray(config.features)).eq(true);
expect(config.features.length).eq(3);
expect(config.features[0]).eq("authentication");
expect(config.features[1]).eq("logging");
expect(config.features[2]).eq("monitoring");
// Should not contain the commented out "experimental-feature"
expect(config.features.includes("experimental-feature")).eq(false);
});

it("should fallback to string value if json with comments parsing fails", async () => {
const invalidJsoncValue = `{
// This is invalid JSON with unclosed bracket
"test": "value"`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.invalid", invalidJsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);

const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get("jsonc.settings.invalid");
expect(config).not.undefined;
expect(typeof config).eq("string", "should fallback to string value");
expect(config).eq(invalidJsoncValue);
});
});