Skip to content

Commit 343bff0

Browse files
authored
Merge hotfix for Azure#35245 (Azure#35285)
* Correctly serialize CertificateIssuer.OrganizationId property Fixes Azure#35245 * Fix CHANGELOG date * Update package version
1 parent 610e932 commit 343bff0

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

sdk/keyvault/Azure.Security.KeyVault.Certificates/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
### Other Changes
1212

13+
## 4.5.1 (2023-03-31)
14+
15+
### Bugs Fixed
16+
17+
- Correctly serialize `CertificateIssuer.OrganizationId` property ([#35245](https://github.com/Azure/azure-sdk-for-net/issues/35245))
18+
1319
## 4.5.0 (2023-03-14)
1420

1521
### Breaking Changes

sdk/keyvault/Azure.Security.KeyVault.Certificates/src/CertificateIssuer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void WriteOrgDetailsProperties(Utf8JsonWriter json)
260260
{
261261
if (!string.IsNullOrEmpty(OrganizationId))
262262
{
263-
json.WriteString(s_organizationIdPropertyNameBytes, AccountId);
263+
json.WriteString(s_organizationIdPropertyNameBytes, OrganizationId);
264264
}
265265

266266
if (!_administratorContacts.IsNullOrEmpty())
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.IO;
6+
using System.Reflection;
7+
using System.Text.Json;
8+
using NUnit.Framework;
9+
10+
namespace Azure.Security.KeyVault.Certificates.Tests
11+
{
12+
public class ModelTests
13+
{
14+
[Test]
15+
public void ValidateCertificateIssuer() =>
16+
AssertModel(() => new CertificateIssuer(nameof(CertificateIssuer.Name), nameof(CertificateIssuer.Provider)));
17+
18+
[Test]
19+
public void ValidateCertificateContact() =>
20+
AssertModel(() => new CertificateContact());
21+
22+
[Test]
23+
public void ValidateCertificatePolicy() =>
24+
AssertModel(() => new CertificatePolicy());
25+
26+
[Test]
27+
public void ValidateIssueProperties() =>
28+
AssertModel(() => new IssuerProperties("Name"));
29+
30+
[Test]
31+
public void ValidateLifetimeAction() =>
32+
AssertModel(() => new LifetimeAction(CertificatePolicyAction.AutoRenew));
33+
34+
#region Assertion helpers
35+
private static void AssertModel<T>(Func<T> factory) where T: IJsonSerializable, IJsonDeserializable
36+
{
37+
T obj = factory();
38+
39+
// Write intrinsic properties.
40+
foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
41+
{
42+
if (property.CanWrite && ShouldSerialize(property, out var value))
43+
{
44+
property.SetValue(obj, value);
45+
}
46+
}
47+
48+
// Serialize.
49+
using MemoryStream ms = new();
50+
using Utf8JsonWriter writer = new(ms);
51+
writer.WriteStartObject();
52+
((IJsonSerializable)obj).WriteProperties(writer);
53+
writer.WriteEndObject();
54+
writer.Flush();
55+
56+
// Deserialize.
57+
ms.Seek(0, SeekOrigin.Begin);
58+
obj = factory();
59+
using JsonDocument doc = JsonDocument.Parse(ms);
60+
((IJsonDeserializable)obj).ReadProperties(doc.RootElement);
61+
62+
// Assert intrinsic properties.
63+
foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
64+
{
65+
// Check CanWrite to make sure we actually wrote it.
66+
if (property.CanWrite && property.CanRead && ShouldSerialize(property, out var value))
67+
{
68+
Assert.AreEqual(value, property.GetValue(obj, null));
69+
}
70+
}
71+
}
72+
73+
private static bool ShouldSerialize(PropertyInfo property, out object value)
74+
{
75+
if (property.PropertyType == typeof(string))
76+
{
77+
value = property.Name;
78+
return true;
79+
}
80+
81+
if (property.PropertyType == typeof(int) || property.PropertyType == typeof(long))
82+
{
83+
value = property.Name.GetHashCode();
84+
return true;
85+
}
86+
87+
if (property.PropertyType == typeof(int?))
88+
{
89+
value = new Nullable<int>(property.Name.GetHashCode());
90+
return true;
91+
}
92+
93+
if (property.PropertyType == typeof(long?))
94+
{
95+
value = new Nullable<long>(property.Name.GetHashCode());
96+
return true;
97+
}
98+
99+
value = string.Empty;
100+
return false;
101+
}
102+
#endregion
103+
}
104+
}

0 commit comments

Comments
 (0)