From 18d78f239cd2420c4aa0b908e4bfe520c5986175 Mon Sep 17 00:00:00 2001 From: Harsha Nalluru Date: Tue, 4 May 2021 19:09:12 -0700 Subject: [PATCH] [App Config] Add the prefix if not present for feature flags (#15136) * Add the prefix if it doesn't start with a featureFlag prefix * serializeFeatureFlagParam for a feature flag with key="abcd" - test * put the typeof check before * delay from recorder * changelog --- .../app-configuration/CHANGELOG.md | 4 + .../app-configuration/src/featureFlag.ts | 3 + .../test/public/featureFlag.spec.ts | 76 +++++++++++-------- .../test/public/index.spec.ts | 4 +- 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/sdk/appconfiguration/app-configuration/CHANGELOG.md b/sdk/appconfiguration/app-configuration/CHANGELOG.md index bbac8d015fbf..294bab62a918 100644 --- a/sdk/appconfiguration/app-configuration/CHANGELOG.md +++ b/sdk/appconfiguration/app-configuration/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History +## 1.2.0-beta.2 (Unreleased) + +- With [#15136](https://github.com/Azure/azure-sdk-for-js/pull/15136), if the key of a feature flag(setting with `contentType="application/vnd.microsoft.appconfig.ff+json;charset=utf-8"`) doesn't start with `".appconfig.featureflag/"` (featureFlagPrefix), SDK adds the prefix before sending the request. + ## 1.2.0-beta.1 (2021-04-06) ### New Features diff --git a/sdk/appconfiguration/app-configuration/src/featureFlag.ts b/sdk/appconfiguration/app-configuration/src/featureFlag.ts index 02c42bfcdac0..52aca569a3d4 100644 --- a/sdk/appconfiguration/app-configuration/src/featureFlag.ts +++ b/sdk/appconfiguration/app-configuration/src/featureFlag.ts @@ -242,6 +242,9 @@ export function deserializeFeatureFlag(setting: ConfigurationSetting): FeatureFl * @internal */ export function serializeFeatureFlagParam(setting: FeatureFlagParam): ConfigurationSettingParam { + if (typeof setting.key === "string" && !setting.key.startsWith(featureFlagPrefix)) { + setting.key = featureFlagPrefix + setting.key; + } const value: JsonFeatureFlag & { id: string } = { id: setting.key.replace(featureFlagPrefix, ""), description: setting.description, diff --git a/sdk/appconfiguration/app-configuration/test/public/featureFlag.spec.ts b/sdk/appconfiguration/app-configuration/test/public/featureFlag.spec.ts index 437695270533..f579efed6ab3 100644 --- a/sdk/appconfiguration/app-configuration/test/public/featureFlag.spec.ts +++ b/sdk/appconfiguration/app-configuration/test/public/featureFlag.spec.ts @@ -16,21 +16,38 @@ import { } from "../../src"; import { Recorder } from "@azure/test-utils-recorder"; import { Context } from "mocha"; +import { serializeFeatureFlagParam } from "../../src/featureFlag"; describe("AppConfigurationClient - FeatureFlag", () => { - let client: AppConfigurationClient; - let recorder: Recorder; + describe("FeatureFlag configuration setting", () => { + let client: AppConfigurationClient; + let recorder: Recorder; - beforeEach(function(this: Context) { - recorder = startRecorder(this); - client = createAppConfigurationClientForTests() || this.skip(); - }); + beforeEach(async function(this: Context) { + recorder = startRecorder(this); + client = createAppConfigurationClientForTests() || this.skip(); + baseSetting = { + conditions: { + clientFilters + }, + enabled: false, + isReadOnly: false, + key: `${featureFlagPrefix + recorder.getUniqueName("name-1")}`, + contentType: featureFlagContentType, + description: "I'm a description", + label: "label-1" + }; + addResponse = await client.addConfigurationSetting(baseSetting); + }); - afterEach(async function(this: Context) { - await recorder.stop(); - }); + afterEach(async function(this: Context) { + await client.deleteConfigurationSetting({ + key: baseSetting.key, + label: baseSetting.label + }); + await recorder.stop(); + }); - describe("FeatureFlag configuration setting", () => { const clientFilters: ( | Record | FeatureFlagTargetingClientFilter @@ -64,28 +81,6 @@ describe("AppConfigurationClient - FeatureFlag", () => { let baseSetting: FeatureFlag; let addResponse: AddConfigurationSettingResponse; - beforeEach(async () => { - baseSetting = { - conditions: { - clientFilters - }, - enabled: false, - isReadOnly: false, - key: `${featureFlagPrefix + recorder.getUniqueName("name-1")}`, - contentType: featureFlagContentType, - description: "I'm a description", - label: "label-1" - }; - addResponse = await client.addConfigurationSetting(baseSetting); - }); - - afterEach(async () => { - await client.deleteConfigurationSetting({ - key: baseSetting.key, - label: baseSetting.label - }); - }); - function assertFeatureFlagProps( actual: Omit, expected: FeatureFlag @@ -192,4 +187,21 @@ describe("AppConfigurationClient - FeatureFlag", () => { await client.deleteConfigurationSetting({ key: secondSetting.key }); }); }); + + describe("FeatureFlag utils", () => { + [featureFlagPrefix + "abcd", "abcd"].forEach((key) => { + it(`serializeFeatureFlagParam for a feature flag with key=${key}`, () => { + assert.equal( + serializeFeatureFlagParam({ + key, + value: `xyz`, + conditions: { clientFilters: [] }, + enabled: false + }).key, + featureFlagPrefix + "abcd", + "Unexpected key in the setting" + ); + }); + }); + }); }); diff --git a/sdk/appconfiguration/app-configuration/test/public/index.spec.ts b/sdk/appconfiguration/app-configuration/test/public/index.spec.ts index 39e1096d3e3d..4674e3633f00 100644 --- a/sdk/appconfiguration/app-configuration/test/public/index.spec.ts +++ b/sdk/appconfiguration/app-configuration/test/public/index.spec.ts @@ -12,8 +12,7 @@ import { startRecorder } from "./utils/testHelpers"; import { AppConfigurationClient, ConfigurationSetting, ConfigurationSettingParam } from "../../src"; -import { delay } from "@azure/core-http"; -import { Recorder } from "@azure/test-utils-recorder"; +import { Recorder, delay } from "@azure/test-utils-recorder"; import { Context } from "mocha"; describe("AppConfigurationClient", () => { @@ -421,7 +420,6 @@ describe("AppConfigurationClient", () => { }); await delay(1000); - await client.setConfigurationSetting({ key, value: "value2"