Skip to content

Commit db9412b

Browse files
tj-devel709TJ Lambertmandel-macaque
authored
[passkit] Add nullability to (generated and manual) bindings (#15262)
* Validating Null ignores and adding tests * add nullability * use is null Co-authored-by: TJ Lambert <[email protected]> Co-authored-by: Manuel de la Pena <[email protected]>
1 parent 3e6fdd8 commit db9412b

File tree

7 files changed

+93
-37
lines changed

7 files changed

+93
-37
lines changed

src/PassKit/PKPaymentRequest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using Foundation;
35
using ObjCRuntime;
@@ -9,13 +11,13 @@ public partial class PKContactFieldsExtensions {
911
static public PKContactFields GetValue (NSSet set)
1012
{
1113
var fields = PKContactFields.None;
12-
if (set == null)
14+
if (set is null)
1315
return fields;
1416

1517
foreach (PKContactFields value in Enum.GetValues (typeof (PKContactFields))) {
1618
var constant = value.GetConstant ();
1719
// None does not have an associated native value and Contains would throw an ANE
18-
if ((constant != null) && set.Contains (constant))
20+
if ((constant is not null) && set.Contains (constant))
1921
fields |= value;
2022
}
2123
return fields;
@@ -31,7 +33,7 @@ static public NSSet GetSet (PKContactFields values)
3133
if (values.HasFlag (value)) {
3234
var constant = value.GetConstant ();
3335
// None does not have an associated native value and Contains would throw an ANE
34-
if (constant != null)
36+
if (constant is not null)
3537
set.Add (constant);
3638
}
3739
}

src/passkit.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ interface PKPassLibrary {
8181
PKPass[] GetPasses ();
8282

8383
[Export ("passWithPassTypeIdentifier:serialNumber:")]
84+
[return: NullAllowed]
8485
PKPass GetPass (string identifier, string serialNumber);
8586

8687
[iOS (8,0)]
@@ -264,7 +265,7 @@ interface PKPayment {
264265
[Deprecated (PlatformName.iOS, 9, 0, message: "Use 'ShippingContact' instead.")]
265266
ABRecord ShippingAddress { get; }
266267

267-
[Export ("shippingMethod", ArgumentSemantic.Strong)]
268+
[NullAllowed, Export ("shippingMethod", ArgumentSemantic.Strong)]
268269
PKShippingMethod ShippingMethod { get; }
269270

270271

@@ -813,7 +814,7 @@ interface PKPass : NSSecureCoding, NSCopying {
813814
[Export ("initWithData:error:")]
814815
NativeHandle Constructor (NSData data, out NSError error);
815816

816-
[Export ("authenticationToken", ArgumentSemantic.Copy)]
817+
[NullAllowed, Export ("authenticationToken", ArgumentSemantic.Copy)]
817818
string AuthenticationToken { get; }
818819

819820
[NoWatch]
@@ -838,16 +839,17 @@ interface PKPass : NSSecureCoding, NSCopying {
838839
[Export ("passURL", ArgumentSemantic.Copy)]
839840
NSUrl PassUrl { get; }
840841

841-
[Export ("relevantDate", ArgumentSemantic.Copy)]
842+
[NullAllowed, Export ("relevantDate", ArgumentSemantic.Copy)]
842843
NSDate RelevantDate { get; }
843844

844845
[Export ("serialNumber", ArgumentSemantic.Copy)]
845846
string SerialNumber { get; }
846847

847-
[Export ("webServiceURL", ArgumentSemantic.Copy)]
848+
[NullAllowed, Export ("webServiceURL", ArgumentSemantic.Copy)]
848849
NSUrl WebServiceUrl { get; }
849850

850851
[Export ("localizedValueForFieldKey:")]
852+
[return: NullAllowed]
851853
NSObject GetLocalizedValue (NSString key); // TODO: Should be enum for PKPassLibraryUserInfoKey
852854

853855
#if !NET
@@ -857,7 +859,7 @@ interface PKPass : NSSecureCoding, NSCopying {
857859
#endif
858860

859861
[iOS (7,0)]
860-
[Export ("userInfo", ArgumentSemantic.Copy)]
862+
[NullAllowed, Export ("userInfo", ArgumentSemantic.Copy)]
861863
NSDictionary UserInfo { get; }
862864

863865
[iOS (8,0)]
@@ -1334,7 +1336,7 @@ interface PKPaymentAuthorizationResult {
13341336
[Export ("status", ArgumentSemantic.Assign)]
13351337
PKPaymentAuthorizationStatus Status { get; set; }
13361338

1337-
[Export ("errors", ArgumentSemantic.Copy)]
1339+
[NullAllowed, Export ("errors", ArgumentSemantic.Copy)]
13381340
NSError[] Errors { get; set; }
13391341
}
13401342

@@ -1372,7 +1374,7 @@ interface PKPaymentRequestShippingContactUpdate {
13721374
[Export ("shippingMethods", ArgumentSemantic.Copy)]
13731375
PKShippingMethod[] ShippingMethods { get; set; }
13741376

1375-
[Export ("errors", ArgumentSemantic.Copy)]
1377+
[NullAllowed, Export ("errors", ArgumentSemantic.Copy)]
13761378
NSError[] Errors { get; set; }
13771379
}
13781380

@@ -1400,7 +1402,7 @@ interface PKPaymentRequestPaymentMethodUpdate {
14001402
NativeHandle Constructor ([NullAllowed] NSError[] errors, PKPaymentSummaryItem [] paymentSummaryItems);
14011403

14021404
[Watch (6,0), iOS (13,0)]
1403-
[Export ("errors", ArgumentSemantic.Copy)]
1405+
[NullAllowed, Export ("errors", ArgumentSemantic.Copy)]
14041406
NSError [] Errors { get; set; }
14051407

14061408
// inlined
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#if !__TVOS__ && !MONOMAC
2+
3+
using System;
4+
using Foundation;
5+
using UIKit;
6+
using PassKit;
7+
using NUnit.Framework;
8+
9+
namespace MonoTouchFixtures.PassKit {
10+
11+
[TestFixture]
12+
[Preserve (AllMembers = true)]
13+
public class PKPassTest {
14+
15+
[Test]
16+
public void GetLocalizedValueNull ()
17+
{
18+
using var pass = new PKPass ();
19+
Assert.IsNull (pass.GetLocalizedValue (new NSString ()), "'PKPass.GetLocalizedValue' is not returning a null value");
20+
}
21+
}
22+
}
23+
24+
#endif

tests/monotouch-test/PassKit/PKPaymentRequestTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ public void WeakRequiredBillingContactFields ()
6464
}
6565
}
6666
}
67+
68+
[Test]
69+
public void CheckDefaultNulls ()
70+
{
71+
using var pr = new PKPaymentRequest ();
72+
Assert.IsNull (pr.CountryCode, "'PKPaymentRequest.CountryCode' is not returning null by default.");
73+
Assert.IsNull (pr.CurrencyCode, "'PKPaymentRequest.CurrencyCode' is not returning null by default.");
74+
Assert.IsNull (pr.MerchantIdentifier, "'PKPaymentRequest.MerchantIdentifier' is not returning null by default.");
75+
Assert.IsNull (pr.PaymentSummaryItems, "'PKPaymentRequest.PaymentSummaryItems' is not returning null by default.");
76+
Assert.IsNull (pr.SupportedNetworks, "'PKPaymentRequest.SupportedNetworks' is not returning null by default.");
77+
78+
Assert.DoesNotThrow (delegate { pr.CountryCode = null; },
79+
"'PKPaymentRequest.CountryCode' cannot be set to null.");
80+
Assert.DoesNotThrow (delegate { pr.CurrencyCode = null; },
81+
"'PKPaymentRequest.CurrencyCode' cannot be set to null.");
82+
Assert.DoesNotThrow (delegate { pr.MerchantIdentifier = null; },
83+
"'PKPaymentRequest.MerchantIdentifier' cannot be set to null.");
84+
Assert.DoesNotThrow (delegate { pr.PaymentSummaryItems = null; },
85+
"'PKPaymentRequest.PaymentSummaryItems' cannot be set to null.");
86+
Assert.DoesNotThrow (delegate { pr.SupportedNetworks = null; },
87+
"'PKPaymentRequest.SupportedNetworks' cannot be set to null.");
88+
}
6789
}
6890
}
6991

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#if !__TVOS__ && !MONOMAC
2+
3+
using System;
4+
using Foundation;
5+
using UIKit;
6+
using PassKit;
7+
using NUnit.Framework;
8+
9+
namespace MonoTouchFixtures.PassKit {
10+
11+
[TestFixture]
12+
[Preserve (AllMembers = true)]
13+
public class PKPaymentSummaryItemTest {
14+
15+
[Test]
16+
public void CheckDefaultNulls ()
17+
{
18+
using var ps = new PKPaymentSummaryItem ();
19+
Assert.IsNull (ps.Amount, "'PKPaymentSummaryItem.Amount' is not returning null by default.");
20+
Assert.IsNull (ps.Label, "'PKPaymentSummaryItem.Label' is not returning null by default.");
21+
22+
Assert.DoesNotThrow (delegate { ps.Amount = null; },
23+
"'PKPaymentSummaryItem.Amount' cannot be set to null.");
24+
Assert.DoesNotThrow (delegate { ps.Label = null; },
25+
"'PKPaymentSummaryItem.Label' cannot be set to null.");
26+
}
27+
}
28+
}
29+
30+
#endif
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
# Initial result from new rule extra-null-allowed
1+
# By default these are null
22
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CountryCode(System.String)' has a extraneous [NullAllowed] on parameter #0
33
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CurrencyCode(System.String)' has a extraneous [NullAllowed] on parameter #0
44
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_MerchantIdentifier(System.String)' has a extraneous [NullAllowed] on parameter #0
55
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_PaymentSummaryItems(PassKit.PKPaymentSummaryItem[])' has a extraneous [NullAllowed] on parameter #0
66
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_SupportedNetworks(Foundation.NSString[])' has a extraneous [NullAllowed] on parameter #0
77
!extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Amount(Foundation.NSDecimalNumber)' has a extraneous [NullAllowed] on parameter #0
88
!extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Label(System.String)' has a extraneous [NullAllowed] on parameter #0
9-
10-
# Initial result from new rule missing-null-allowed
11-
!missing-null-allowed! 'Foundation.NSDate PassKit.PKPass::get_RelevantDate()' is missing an [NullAllowed] on return type
12-
!missing-null-allowed! 'Foundation.NSDictionary PassKit.PKPass::get_UserInfo()' is missing an [NullAllowed] on return type
13-
!missing-null-allowed! 'Foundation.NSObject PassKit.PKPass::GetLocalizedValue(Foundation.NSString)' is missing an [NullAllowed] on return type
14-
!missing-null-allowed! 'Foundation.NSUrl PassKit.PKPass::get_WebServiceUrl()' is missing an [NullAllowed] on return type
15-
!missing-null-allowed! 'PassKit.PKPass PassKit.PKPassLibrary::GetPass(System.String,System.String)' is missing an [NullAllowed] on return type
16-
!missing-null-allowed! 'PassKit.PKShippingMethod PassKit.PKPayment::get_ShippingMethod()' is missing an [NullAllowed] on return type
17-
!missing-null-allowed! 'System.String PassKit.PKPass::get_AuthenticationToken()' is missing an [NullAllowed] on return type
18-
!missing-null-allowed! 'System.Void PassKit.PKPaymentAuthorizationResult::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0
19-
!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestPaymentMethodUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0
20-
!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestShippingContactUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0

tests/xtro-sharpie/common-PassKit.ignore

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,11 @@
55
!incorrect-protocol-member! PKPaymentAuthorizationControllerDelegate::paymentAuthorizationController:didAuthorizePayment:completion: is OPTIONAL and should NOT be abstract
66
!incorrect-protocol-member! PKPaymentAuthorizationViewControllerDelegate::paymentAuthorizationViewControllerWillAuthorizePayment: is OPTIONAL and should NOT be abstract
77

8-
# Initial result from new rule extra-null-allowed
8+
# By default these are null
99
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CountryCode(System.String)' has a extraneous [NullAllowed] on parameter #0
1010
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CurrencyCode(System.String)' has a extraneous [NullAllowed] on parameter #0
1111
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_MerchantIdentifier(System.String)' has a extraneous [NullAllowed] on parameter #0
1212
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_PaymentSummaryItems(PassKit.PKPaymentSummaryItem[])' has a extraneous [NullAllowed] on parameter #0
1313
!extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_SupportedNetworks(Foundation.NSString[])' has a extraneous [NullAllowed] on parameter #0
1414
!extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Amount(Foundation.NSDecimalNumber)' has a extraneous [NullAllowed] on parameter #0
1515
!extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Label(System.String)' has a extraneous [NullAllowed] on parameter #0
16-
17-
# Initial result from new rule missing-null-allowed
18-
!missing-null-allowed! 'Foundation.NSDate PassKit.PKPass::get_RelevantDate()' is missing an [NullAllowed] on return type
19-
!missing-null-allowed! 'Foundation.NSDictionary PassKit.PKPass::get_UserInfo()' is missing an [NullAllowed] on return type
20-
!missing-null-allowed! 'Foundation.NSObject PassKit.PKPass::GetLocalizedValue(Foundation.NSString)' is missing an [NullAllowed] on return type
21-
!missing-null-allowed! 'Foundation.NSUrl PassKit.PKPass::get_WebServiceUrl()' is missing an [NullAllowed] on return type
22-
!missing-null-allowed! 'PassKit.PKPass PassKit.PKPassLibrary::GetPass(System.String,System.String)' is missing an [NullAllowed] on return type
23-
!missing-null-allowed! 'PassKit.PKShippingMethod PassKit.PKPayment::get_ShippingMethod()' is missing an [NullAllowed] on return type
24-
!missing-null-allowed! 'System.String PassKit.PKPass::get_AuthenticationToken()' is missing an [NullAllowed] on return type
25-
!missing-null-allowed! 'System.Void PassKit.PKPaymentAuthorizationResult::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0
26-
!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestPaymentMethodUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0
27-
!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestShippingContactUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0

0 commit comments

Comments
 (0)