Skip to content

Commit 6fce7e2

Browse files
Correct deserialization of SessionStateAction for Cosmos provider. (#72)
1 parent 3f3a094 commit 6fce7e2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/CosmosDBSessionStateProviderAsync/SessionStateActionsConverter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public override bool CanConvert(Type typeToConvert)
2626
throw new ArgumentException("reader");
2727
}
2828

29+
// See the note below in the writer for how to map this flags enum from a boolean.
2930
return reader.GetBoolean() ? SessionStateActions.InitializeItem : SessionStateActions.None;
3031
}
3132

@@ -37,7 +38,12 @@ public override void Write(Utf8JsonWriter writer, SessionStateActions? action, J
3738
return;
3839
}
3940

40-
writer.WriteBooleanValue((action == SessionStateActions.None));
41+
// SessionStateActions is a [Flags] enum. The SQL providers serialize it as flags. I don't know why we didn't
42+
// just go with int or something flag-like here instead of true/false.
43+
// 'None' means that the initialization of this state item has already been done and
44+
// thus the item is considered initialized. We serialize this item with the field name
45+
// "uninitialized", so 'None' should map to false.
46+
writer.WriteBooleanValue((action != SessionStateActions.None));
4147
}
4248
}
4349
}

test/Microsoft.AspNet.SessionState.CosmosDBSessionStateProviderAsync.Test/SessionStateItemTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void SessionStateItem_Can_Be_Serialized_With_Null_LockAge()
2828
Timeout = 10
2929
};
3030
var json = JsonSerializer.Serialize<SessionStateItem>(item);
31-
var expected = string.Format(JsonTemplate, item.SessionId, "null", item.LockCookie, item.Timeout, item.Locked, "AQE=", true);
31+
var expected = string.Format(JsonTemplate, item.SessionId, "null", item.LockCookie, item.Timeout, item.Locked, "AQE=", false);
3232

3333
Assert.Equal(expected, json, true);
3434
}
@@ -47,7 +47,7 @@ public void SessionStateItem_Can_Be_Serialized_With_LockAge()
4747
Timeout = 10
4848
};
4949
var json = JsonSerializer.Serialize(item);
50-
var expected = string.Format(JsonTemplate, item.SessionId, 60 * 1, item.LockCookie, item.Timeout, item.Locked, "AQE=", false);
50+
var expected = string.Format(JsonTemplate, item.SessionId, 60 * 1, item.LockCookie, item.Timeout, item.Locked, "AQE=", true);
5151

5252
Assert.Equal(expected, json, true);
5353
}

0 commit comments

Comments
 (0)