Skip to content

Commit 233553f

Browse files
sort json key
1 parent 6df1970 commit 233553f

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

src/AzureAppConfigurationImpl.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IKeyValueAdapter } from "./IKeyValueAdapter";
99
import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter";
1010
import { DEFAULT_REFRESH_INTERVAL_IN_MS, MIN_REFRESH_INTERVAL_IN_MS } from "./RefreshOptions";
1111
import { Disposable } from "./common/disposable";
12+
import { base64Helper, jsonSorter } from "./common/utils";
1213
import {
1314
FEATURE_FLAGS_KEY_NAME,
1415
FEATURE_MANAGEMENT_KEY_NAME,
@@ -690,7 +691,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
690691

691692
const variantConfiguration: string[] = [];
692693
for (const variant of sortedVariantsList) {
693-
const configurationValue = JSON.stringify(variant[CONFIGURATION_VALUE_KEY_NAME], null, 0) ?? "";
694+
const configurationValue = JSON.stringify(variant[CONFIGURATION_VALUE_KEY_NAME], jsonSorter) ?? "";
694695
variantConfiguration.push(`${base64Helper(variant[NAME_KEY_NAME])},${configurationValue}`);
695696
}
696697
rawAllocationId += variantConfiguration.join(";");
@@ -750,15 +751,6 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
750751
}
751752
}
752753

753-
function base64Helper(str: string): string {
754-
const bytes = new TextEncoder().encode(str); // UTF-8 encoding
755-
let chars = "";
756-
for (let i = 0; i < bytes.length; i++) {
757-
chars += String.fromCharCode(bytes[i]);
758-
}
759-
return btoa(chars);
760-
}
761-
762754
function getValidSelectors(selectors: SettingSelector[]): SettingSelector[] {
763755
// below code deduplicates selectors by keyFilter and labelFilter, the latter selector wins
764756
const uniqueSelectors: SettingSelector[] = [];

src/common/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
export function base64Helper(str: string): string {
5+
const bytes = new TextEncoder().encode(str); // UTF-8 encoding
6+
let chars = "";
7+
for (let i = 0; i < bytes.length; i++) {
8+
chars += String.fromCharCode(bytes[i]);
9+
}
10+
return btoa(chars);
11+
}
12+
13+
export function jsonSorter(key, value) {
14+
if (value === null) {
15+
return null;
16+
}
17+
if (Array.isArray(value)) {
18+
return value;
19+
}
20+
if (typeof value === "object") {
21+
return Object.fromEntries(Object.entries(value).sort());
22+
}
23+
return value;
24+
}

test/featureFlag.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ const mockedKVs = [{
111111
seed: "123"
112112
}
113113
}),
114+
createMockedFeatureFlag("TelemetryVariantPercentile", {
115+
enabled: true,
116+
telemetry: { enabled: true },
117+
variants: [
118+
{
119+
name: "True_Override",
120+
configuration_value: {
121+
someOtherKey: {
122+
someSubKey: "someSubValue"
123+
},
124+
someKey4: [3, 1, 4, true],
125+
someKey: "someValue",
126+
someKey3: 3.14,
127+
someKey2: 3
128+
}
129+
}
130+
],
131+
allocation: {
132+
default_when_enabled: "True_Override",
133+
percentile: [
134+
{
135+
variant: "True_Override",
136+
from: 0,
137+
to: 100
138+
}
139+
]
140+
}
141+
})
114142
]);
115143

116144
describe("feature flags", function () {
@@ -305,6 +333,10 @@ describe("feature flags", function () {
305333

306334
const ComplexConfigurationValue = (featureFlags as any[]).find(item => item.id === "ComplexConfigurationValue");
307335
expect(ComplexConfigurationValue).not.undefined;
308-
expect(ComplexConfigurationValue?.telemetry.metadata.AllocationId).equals("72A0b4sZ5HSAtaQxFe73");
336+
expect(ComplexConfigurationValue?.telemetry.metadata.AllocationId).equals("4Bes0AlwuO8kYX-YkBWs");
337+
338+
const TelemetryVariantPercentile = (featureFlags as any[]).find(item => item.id === "TelemetryVariantPercentile");
339+
expect(TelemetryVariantPercentile).not.undefined;
340+
expect(TelemetryVariantPercentile?.telemetry.metadata.AllocationId).equals("YsdJ4pQpmhYa8KEhRLUn");
309341
});
310342
});

0 commit comments

Comments
 (0)