Skip to content
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

bug: Handle null, empty, or whitespaces for RolloutId #304

Merged
merged 5 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions OptimizelySDK.Tests/EmptyRolloutRule.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
],
"id": "18244658520",
"key": "empty_rollout"
},
{
"experimentIds": [],
"rolloutId": "",
"variables": [
{
"defaultValue": "",
"type": "string",
"id": "2832355113",
"key": "var_2"
}
],
"id": "24246538512",
"key": "empty_rollout_id"
}
],
"experiments": [],
Expand Down
48 changes: 47 additions & 1 deletion OptimizelySDK.Tests/ProjectConfigTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021, Optimizely
* Copyright 2017-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -822,5 +822,51 @@ public void TestIsFeatureExperimentReturnsTrueForExperimentThatBelongsToAFeature

Assert.True(typedConfig.IsFeatureExperiment(experiment.Id));
}

[Test]
public void TestRolloutWithEmptyStringRolloutIdFromConfigFile()
{
var projectConfig = DatafileProjectConfig.Create(TestData.EmptyRolloutDatafile, null, null);
Assert.IsNotNull(projectConfig);
var featureFlag = projectConfig.FeatureKeyMap["empty_rollout_id"];

var rollout = projectConfig.GetRolloutFromId(featureFlag.RolloutId);

Assert.IsNull(rollout.Experiments);
Assert.IsNull(rollout.Id);
}

[Test]
public void TestRolloutWithEmptyStringRolloutId()
{
var rolloutId = string.Empty;

var rollout = Config.GetRolloutFromId(rolloutId);

Assert.IsNull(rollout.Experiments);
Assert.IsNull(rollout.Id);
}

[Test]
public void TestRolloutWithConsistingOfASingleSpaceRolloutId()
{
var rolloutId = " "; // single space

var rollout = Config.GetRolloutFromId(rolloutId);

Assert.IsNull(rollout.Experiments);
Assert.IsNull(rollout.Id);
}

[Test]
public void TestRolloutWithConsistingOfANullRolloutId()
{
string nullRolloutId = null;

var rollout = Config.GetRolloutFromId(nullRolloutId);

Assert.IsNull(rollout.Experiments);
Assert.IsNull(rollout.Id);
}
}
}
7 changes: 6 additions & 1 deletion OptimizelySDK.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EMPTY_BLOCK_STYLE/@EntryValue">TOGETHER_SAME_LINE</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_EXPR_MEMBER_ARRANGEMENT/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_INITIALIZER_ARRANGEMENT/@EntryValue">False</s:Boolean>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is causing linter to cause errors. not sure.

<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/MAX_INITIALIZER_ELEMENTS_ON_LINE/@EntryValue">0</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/OTHER_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSOR_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
Expand Down Expand Up @@ -40,4 +41,8 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"&gt;&lt;ExtraRule Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Interfaces/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="I" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If .DotSettings is causing problems should I add JetBrains to .gitignore?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not problematic, just end of line is better to have at the end of file.

11 changes: 10 additions & 1 deletion OptimizelySDK/Config/DatafileProjectConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021, Optimizely
* Copyright 2019-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -636,6 +636,15 @@ public Variation GetFlagVariationByKey(string flagKey, string variationKey)
/// <returns>Rollout Entity corresponding to the rollout ID or a dummy entity if ID is invalid</returns>
public Rollout GetRolloutFromId(string rolloutId)
{
#if NET35 || NET40
if (string.IsNullOrEmpty(rolloutId) || string.IsNullOrEmpty(rolloutId.Trim()))
#else
if (string.IsNullOrWhiteSpace(rolloutId))
#endif
{
return new Rollout();
}

if (_RolloutIdMap.ContainsKey(rolloutId))
return _RolloutIdMap[rolloutId];

Expand Down