#12 take care the ftPayItemId is null if empty to avoid issues with b…#13
Conversation
…ackend API (reject because of 400 BAD REUEST on /sign)
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #12 by ensuring that ftPayItemId is null (rather than an empty string) when no ID is set, to prevent problems with the backend when an empty string is sent in the JSON payload.
Changes:
ftPayItemIdinPayItemis changed to a nullablestring?type to allownullvalues.- A normalization loop is added to the
ReceiptRequestconstructor to convert any emptyftPayItemIdvalues tonullbefore the pay items are stored.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
libPosSystemAPI/DTO/PayItem.cs |
Changes ftPayItemId to string? to allow null, but retains = string.Empty as the default initializer |
libPosSystemAPI/DTO/ReceiptRequest.cs |
Adds a loop in the constructor to normalize empty ftPayItemId strings to null |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| { | ||
| [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] | ||
| public string ftPayItemId { get; set; } = string.Empty; | ||
| public string? ftPayItemId { get; set; } = string.Empty; |
There was a problem hiding this comment.
The ftPayItemId property is now declared as string? but the initializer is still = string.Empty. Since [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] only omits a property when its value equals the type's default (null for reference types), an instance of PayItem where ftPayItemId is never set will still serialize the property as "" — not omit it. This defeats the purpose of the change, because any PayItem that isn't processed through the ReceiptRequest constructor (e.g. deserialized from JSON and re-serialized, or directly constructed outside of ReceiptRequest) will still emit "ftPayItemId": "" in the JSON payload.
The default initializer should be changed from = string.Empty to = null (or simply removed, since null is already the implicit default for string?), so that the [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] attribute works correctly and the property is omitted from serialization when no ID is assigned.
| public string? ftPayItemId { get; set; } = string.Empty; | |
| public string? ftPayItemId { get; set; } |
| // verify that empty ftPayItemIDs are null | ||
| foreach (var payItem in cbPayItems) | ||
| { | ||
| if (payItem.ftPayItemId == "") | ||
| { | ||
| payItem.ftPayItemId = null; | ||
| } | ||
| } |
There was a problem hiding this comment.
The normalization loop in the constructor mutates the PayItem objects in the caller-provided cbPayItems list in-place (setting ftPayItemId = null on the original objects). This is an unexpected side effect: callers may not expect that passing their list into the ReceiptRequest constructor will modify their PayItem objects. If the root issue (see PayItem.cs line 9) is fixed by changing the default initializer to null, then this normalization loop would become unnecessary and could be removed entirely.
| // verify that empty ftPayItemIDs are null | |
| foreach (var payItem in cbPayItems) | |
| { | |
| if (payItem.ftPayItemId == "") | |
| { | |
| payItem.ftPayItemId = null; | |
| } | |
| } |
No description provided.