Skip to content

Commit

Permalink
Better handle reaonly and writeonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBenzSW committed Jul 17, 2024
1 parent 730bb77 commit 0606dff
Show file tree
Hide file tree
Showing 302 changed files with 2,113 additions and 1,803 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,12 @@ Updated nuget package dependencies

## Changed

- Add more explicit nullable reference types
- Add more explicit nullable reference types

## 3.0.0-beta.3

## Changed

- Don't require any properties for deserialization so that small API changes don't completely break consumers
- Don't try to serialize read-only properties
- Don't try to deserialize write-only properties
12 changes: 5 additions & 7 deletions ShipEngineSDK.Test/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ public void WhenSerializing_ItShouldIncludeWriteOnlyProperties()
public void WhenDeserializing_ItShouldNotIncludeWriteOnlyProperties()
{
var json = @"{
""pickup_window"": {
""end_at"": ""2024-07-19T12:27:35+00:00"",
""start_at"": ""2024-07-17T12:27:35+00:00""
}
""label_download_type"": ""inline"",
""label_format"": ""png""
}";

var pickup = JsonSerializer.Deserialize<SchedulePickupRequestBody>(json, ShipEngineClient.JsonSerializerOptions);
var pickup = JsonSerializer.Deserialize<CreateReturnLabelRequestBody>(json, ShipEngineClient.JsonSerializerOptions);

Assert.Equal("se-12345", pickup.CarrierId);
Assert.Null(pickup.PickupWindow);
Assert.Equal(LabelFormat.Png, pickup.LabelFormat);
Assert.Null(pickup.LabelDownloadType);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions ShipEngineSDK/JsonWriteOnlyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace ShipEngineSDK;

/// <summary>
/// Marks a property as write only, which means it won't be deserialized
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
internal class JsonWriteOnlyAttribute : Attribute
{

}
2 changes: 1 addition & 1 deletion ShipEngineSDK/Model/AccountSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public partial class AccountSettings
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AccountSettings {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" DefaultLabelLayout: ").Append(DefaultLabelLayout).Append("\n");
Expand Down
16 changes: 8 additions & 8 deletions ShipEngineSDK/Model/AccountSettingsImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ public override bool CanConvert(Type typeToConvert) =>
}


/// <summary>
/// The image type
/// </summary>
/// <value>The image type</value>
[JsonPropertyName("image_content_type"), JsonPropertyOrder(2)]
public ImageContentTypeEnum? ImageContentType { get; set; }

/// <summary>
/// The date and time that the image was created in ShipEngine.
/// </summary>
Expand All @@ -105,6 +98,13 @@ public override bool CanConvert(Type typeToConvert) =>
[JsonPropertyName("created_at"), JsonInclude]
public DateTimeOffset? CreatedAt { get; private set; }

/// <summary>
/// The image type
/// </summary>
/// <value>The image type</value>
[JsonPropertyName("image_content_type"), JsonPropertyOrder(2)]
public ImageContentTypeEnum? ImageContentType { get; set; }

/// <summary>
/// A base64 encoded string representation of the image.
/// </summary>
Expand Down Expand Up @@ -162,7 +162,7 @@ public override bool CanConvert(Type typeToConvert) =>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AccountSettingsImages {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" CreatedAt: ").Append(CreatedAt).Append("\n");
Expand Down
6 changes: 3 additions & 3 deletions ShipEngineSDK/Model/AddFundsToCarrierRequestBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public partial class AddFundsToCarrierRequestBody
/// The monetary amount, in the specified currency.
/// </summary>
/// <value>The monetary amount, in the specified currency.</value>
[JsonPropertyName("amount"), JsonRequired, JsonPropertyOrder(1)]
[JsonPropertyName("amount"), JsonPropertyOrder(1)]
public required double Amount { get; set; }

/// <summary>
/// The currencies that are supported by ShipEngine are the ones that specified by ISO 4217: https://www.iso.org/iso-4217-currency-codes.html
/// </summary>
/// <value>The currencies that are supported by ShipEngine are the ones that specified by ISO 4217: https://www.iso.org/iso-4217-currency-codes.html </value>
[JsonPropertyName("currency"), JsonRequired, JsonPropertyOrder(2)]
[JsonPropertyName("currency"), JsonPropertyOrder(2)]
public required string Currency { get; set; }


Expand All @@ -49,7 +49,7 @@ public partial class AddFundsToCarrierRequestBody
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AddFundsToCarrierRequestBody {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" Amount: ").Append(Amount).Append("\n");
Expand Down
2 changes: 1 addition & 1 deletion ShipEngineSDK/Model/AddFundsToCarrierResponseBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public partial class AddFundsToCarrierResponseBody
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AddFundsToCarrierResponseBody {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" Balance: ").Append(Balance).Append("\n");
Expand Down
6 changes: 3 additions & 3 deletions ShipEngineSDK/Model/AddFundsToInsuranceRequestBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public partial class AddFundsToInsuranceRequestBody
/// The monetary amount, in the specified currency.
/// </summary>
/// <value>The monetary amount, in the specified currency.</value>
[JsonPropertyName("amount"), JsonRequired, JsonPropertyOrder(1)]
[JsonPropertyName("amount"), JsonPropertyOrder(1)]
public required double Amount { get; set; }

/// <summary>
/// The currencies that are supported by ShipEngine are the ones that specified by ISO 4217: https://www.iso.org/iso-4217-currency-codes.html
/// </summary>
/// <value>The currencies that are supported by ShipEngine are the ones that specified by ISO 4217: https://www.iso.org/iso-4217-currency-codes.html </value>
[JsonPropertyName("currency"), JsonRequired, JsonPropertyOrder(2)]
[JsonPropertyName("currency"), JsonPropertyOrder(2)]
public required string Currency { get; set; }


Expand All @@ -49,7 +49,7 @@ public partial class AddFundsToInsuranceRequestBody
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AddFundsToInsuranceRequestBody {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" Amount: ").Append(Amount).Append("\n");
Expand Down
6 changes: 3 additions & 3 deletions ShipEngineSDK/Model/AddFundsToInsuranceResponseBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public partial class AddFundsToInsuranceResponseBody
/// The monetary amount, in the specified currency.
/// </summary>
/// <value>The monetary amount, in the specified currency.</value>
[JsonPropertyName("amount"), JsonRequired, JsonPropertyOrder(1)]
[JsonPropertyName("amount"), JsonPropertyOrder(1)]
public required double Amount { get; set; }

/// <summary>
/// The currencies that are supported by ShipEngine are the ones that specified by ISO 4217: https://www.iso.org/iso-4217-currency-codes.html
/// </summary>
/// <value>The currencies that are supported by ShipEngine are the ones that specified by ISO 4217: https://www.iso.org/iso-4217-currency-codes.html </value>
[JsonPropertyName("currency"), JsonRequired, JsonPropertyOrder(2)]
[JsonPropertyName("currency"), JsonPropertyOrder(2)]
public required string Currency { get; set; }


Expand All @@ -49,7 +49,7 @@ public partial class AddFundsToInsuranceResponseBody
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AddFundsToInsuranceResponseBody {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" Amount: ").Append(Amount).Append("\n");
Expand Down
2 changes: 1 addition & 1 deletion ShipEngineSDK/Model/AddToBatchRequestBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public partial class AddToBatchRequestBody
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AddToBatchRequestBody {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" RateIds: ").Append(RateIds).Append("\n");
Expand Down
30 changes: 15 additions & 15 deletions ShipEngineSDK/Model/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ namespace ShipEngineSDK.Model;
public partial class Address
{

/// <summary>
/// Indicates whether this is a residential address.
/// </summary>
/// <value>Indicates whether this is a residential address.</value>
[JsonPropertyName("address_residential_indicator"), JsonRequired, JsonPropertyOrder(2)]
public required AddressResidentialIndicator AddressResidentialIndicator { get; set; }

/// <summary>
/// The first line of the street address. For some addresses, this may be the only line. Other addresses may require 2 or 3 lines.
/// </summary>
/// <value>The first line of the street address. For some addresses, this may be the only line. Other addresses may require 2 or 3 lines. </value>
/// <example>
/// 1999 Bishop Grandin Blvd.
/// </example>
[JsonPropertyName("address_line1"), JsonRequired, JsonPropertyOrder(1)]
[JsonPropertyName("address_line1"), JsonPropertyOrder(1)]
public required string AddressLine1 { get; set; }

/// <summary>
/// Indicates whether this is a residential address.
/// </summary>
/// <value>Indicates whether this is a residential address.</value>
[JsonPropertyName("address_residential_indicator"), JsonPropertyOrder(2)]
public required AddressResidentialIndicator AddressResidentialIndicator { get; set; }

/// <summary>
/// The name of the city or locality
/// </summary>
/// <value>The name of the city or locality</value>
/// <example>
/// Winnipeg
/// </example>
[JsonPropertyName("city_locality"), JsonRequired, JsonPropertyOrder(3)]
[JsonPropertyName("city_locality"), JsonPropertyOrder(3)]
public required string CityLocality { get; set; }

/// <summary>
Expand All @@ -62,7 +62,7 @@ public partial class Address
/// <example>
/// CA
/// </example>
[JsonPropertyName("country_code"), JsonRequired, JsonPropertyOrder(4)]
[JsonPropertyName("country_code"), JsonPropertyOrder(4)]
public required string CountryCode { get; set; }

/// <summary>
Expand All @@ -72,7 +72,7 @@ public partial class Address
/// <example>
/// John Doe
/// </example>
[JsonPropertyName("name"), JsonRequired, JsonPropertyOrder(5)]
[JsonPropertyName("name"), JsonPropertyOrder(5)]
public required string Name { get; set; }

/// <summary>
Expand All @@ -82,7 +82,7 @@ public partial class Address
/// <example>
/// +1 204-253-9411 ext. 123
/// </example>
[JsonPropertyName("phone"), JsonRequired, JsonPropertyOrder(6)]
[JsonPropertyName("phone"), JsonPropertyOrder(6)]
public required string Phone { get; set; }

/// <summary>
Expand All @@ -92,7 +92,7 @@ public partial class Address
/// <example>
/// 78756-3717
/// </example>
[JsonPropertyName("postal_code"), JsonRequired, JsonPropertyOrder(7)]
[JsonPropertyName("postal_code"), JsonPropertyOrder(7)]
public required string PostalCode { get; set; }

/// <summary>
Expand All @@ -102,7 +102,7 @@ public partial class Address
/// <example>
/// Manitoba
/// </example>
[JsonPropertyName("state_province"), JsonRequired, JsonPropertyOrder(8)]
[JsonPropertyName("state_province"), JsonPropertyOrder(8)]
public required string StateProvince { get; set; }

/// <summary>
Expand Down Expand Up @@ -152,7 +152,7 @@ public partial class Address
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class Address {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" AddressLine1: ").Append(AddressLine1).Append("\n");
Expand Down
5 changes: 5 additions & 0 deletions ShipEngineSDK/Model/AddressResidentialIndicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public AddressResidentialIndicator(string value)
/// </summary>
public override bool Equals(object? obj) =>
obj is AddressResidentialIndicator other && _value == other._value;

/// <summary>
/// Get the hash code of the object
/// </summary>
public override int GetHashCode() => _value.GetHashCode();
}

internal class AddressResidentialIndicatorJsonConverter : JsonConverter<AddressResidentialIndicator>
Expand Down
24 changes: 12 additions & 12 deletions ShipEngineSDK/Model/AddressToValidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,14 @@ namespace ShipEngineSDK.Model;
public partial class AddressToValidate
{

/// <summary>
/// Indicates whether this is a residential address.
/// </summary>
/// <value>Indicates whether this is a residential address.</value>
[JsonPropertyName("address_residential_indicator"), JsonPropertyOrder(7)]
public AddressResidentialIndicator? AddressResidentialIndicator { get; set; }

/// <summary>
/// The first line of the street address. For some addresses, this may be the only line. Other addresses may require 2 or 3 lines.
/// </summary>
/// <value>The first line of the street address. For some addresses, this may be the only line. Other addresses may require 2 or 3 lines. </value>
/// <example>
/// 1999 Bishop Grandin Blvd.
/// </example>
[JsonPropertyName("address_line1"), JsonRequired, JsonPropertyOrder(1)]
[JsonPropertyName("address_line1"), JsonPropertyOrder(1)]
public required string AddressLine1 { get; set; }

/// <summary>
Expand All @@ -52,7 +45,7 @@ public partial class AddressToValidate
/// <example>
/// Winnipeg
/// </example>
[JsonPropertyName("city_locality"), JsonRequired, JsonPropertyOrder(2)]
[JsonPropertyName("city_locality"), JsonPropertyOrder(2)]
public required string CityLocality { get; set; }

/// <summary>
Expand All @@ -62,7 +55,7 @@ public partial class AddressToValidate
/// <example>
/// CA
/// </example>
[JsonPropertyName("country_code"), JsonRequired, JsonPropertyOrder(3)]
[JsonPropertyName("country_code"), JsonPropertyOrder(3)]
public required string CountryCode { get; set; }

/// <summary>
Expand All @@ -72,7 +65,7 @@ public partial class AddressToValidate
/// <example>
/// Manitoba
/// </example>
[JsonPropertyName("state_province"), JsonRequired, JsonPropertyOrder(4)]
[JsonPropertyName("state_province"), JsonPropertyOrder(4)]
public required string StateProvince { get; set; }

/// <summary>
Expand All @@ -95,6 +88,13 @@ public partial class AddressToValidate
[JsonPropertyName("address_line3"), JsonPropertyOrder(6)]
public string? AddressLine3 { get; set; }

/// <summary>
/// Indicates whether this is a residential address.
/// </summary>
/// <value>Indicates whether this is a residential address.</value>
[JsonPropertyName("address_residential_indicator"), JsonPropertyOrder(7)]
public AddressResidentialIndicator? AddressResidentialIndicator { get; set; }

/// <summary>
/// If this is a business address, then the company name should be specified here.
/// </summary>
Expand Down Expand Up @@ -152,7 +152,7 @@ public partial class AddressToValidate
/// <returns>String presentation of the object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb.Append("class AddressToValidate {\n");
#pragma warning disable CS0612 // Type or member is obsolete
sb.Append(" AddressLine1: ").Append(AddressLine1).Append("\n");
Expand Down
Loading

0 comments on commit 0606dff

Please sign in to comment.