Home > Software design >  Missing properties when adding document to Azure Cosmos DB
Missing properties when adding document to Azure Cosmos DB

Time:11-16

I am new with C# .net core and I am experimenting with creating azure functions.

I am extending a class (BalanceTransaction) from the stripe package.

public class BalanceTransaction : StripeEntity<BalanceTransaction>, IHasId, IHasObject
{
    public BalanceTransaction();

    //
    // Summary:
    //     (Expanded) The Stripe object to which this transaction is related. For more information,
    //     see the expand documentation.
    [JsonIgnore]
    public IBalanceTransactionSource Source { get; set; }
    //
    // Summary:
    //     (ID of the IBalanceTransactionSource) The Stripe object to which this transaction
    //     is related.
    [JsonIgnore]
    public string SourceId { get; set; }
    //
    // Summary:
    //     Learn more about how reporting categories can help you understand balance transactions
    //     from an accounting perspective.
    [JsonProperty("reporting_category")]
    public string ReportingCategory { get; set; }
    //
    // Summary:
    //     Net amount of the transaction, in %s.
    [JsonProperty("net")]
    public long Net { get; set; }
    //
    // Summary:
    //     Detailed breakdown of fees (in %s) paid for this transaction.
    [JsonProperty("fee_details")]
    public List<BalanceTransactionFeeDetail> FeeDetails { get; set; }
    //
    // Summary:
    //     Fees (in %s) paid for this transaction.
    [JsonProperty("fee")]
    public long Fee { get; set; }
    //
    // Summary:
    //     If the transaction's net funds are available in the Stripe balance yet. Either
    //     available or pending.
    [JsonProperty("status")]
    public string Status { get; set; }
    //
    // Summary:
    //     The exchange rate used, if applicable, for this transaction. Specifically, if
    //     money was converted from currency A to currency B, then the amount in currency
    //     A, times exchange_rate, would be the amount in currency B. For example, suppose
    //     you charged a customer 10.00 EUR. Then the PaymentIntent's amount would be 1000
    //     and currency would be eur. Suppose this was converted into 12.34 USD in your
    //     Stripe account. Then the BalanceTransaction's amount would be 1234, currency
    //     would be usd, and exchange_rate would be 1.234.
    [JsonProperty("exchange_rate")]
    public decimal? ExchangeRate { get; set; }
    //
    // Summary:
    //     Three-letter ISO currency code, in lowercase. Must be a supported currency.
    [JsonProperty("currency")]
    public string Currency { get; set; }
    //
    // Summary:
    //     Time at which the object was created. Measured in seconds since the Unix epoch.
    [JsonConverter(typeof(UnixDateTimeConverter))]
    [JsonProperty("created")]
    public DateTime Created { get; set; }
    //
    // Summary:
    //     The date the transaction's net funds will become available in the Stripe balance.
    [JsonConverter(typeof(UnixDateTimeConverter))]
    [JsonProperty("available_on")]
    public DateTime AvailableOn { get; set; }
    //
    // Summary:
    //     Gross amount of the transaction, in %s.
    [JsonProperty("amount")]
    public long Amount { get; set; }
    //
    // Summary:
    //     String representing the object's type. Objects of the same type share the same
    //     value.
    [JsonProperty("object")]
    public string Object { get; set; }
    //
    // Summary:
    //     Unique identifier for the object.
    [JsonProperty("id")]
    public string Id { get; set; }
    //
    // Summary:
    //     An arbitrary string attached to the object. Often useful for displaying to users.
    [JsonProperty("description")]
    public string Description { get; set; }
    //
    // Summary:
    //     Transaction type: adjustment, advance, advance_funding, anticipation_repayment,
    //     application_fee, application_fee_refund, charge, connect_collection_transfer,
    //     contribution, issuing_authorization_hold, issuing_authorization_release, issuing_dispute,
    //     issuing_transaction, payment, payment_failure_refund, payment_refund, payout,
    //     payout_cancel, payout_failure, refund, refund_failure, reserve_transaction, reserved_funds,
    //     stripe_fee, stripe_fx_fee, tax_fee, topup, topup_reversal, transfer, transfer_cancel,
    //     transfer_failure, or transfer_refund. Learn more about balance transaction types
    //     and what they represent. If you are looking to classify transactions for accounting
    //     purposes, you might want to consider reporting_category instead. One of: adjustment,
    //     advance, advance_funding, anticipation_repayment, application_fee, application_fee_refund,
    //     charge, connect_collection_transfer, contribution, issuing_authorization_hold,
    //     issuing_authorization_release, issuing_dispute, issuing_transaction, payment,
    //     payment_failure_refund, payment_refund, payout, payout_cancel, payout_failure,
    //     refund, refund_failure, reserve_transaction, reserved_funds, stripe_fee, stripe_fx_fee,
    //     tax_fee, topup, topup_reversal, transfer, transfer_cancel, transfer_failure,
    //     or transfer_refund.
    [JsonProperty("type")]
    public string Type { get; set; }
}

My extension class contains two extra properties

public class CustomBalanceTransaction : BalanceTransaction
{
    [JsonPropertyName("customerid")]
    public string Customerid { get; set; }

    [JsonPropertyName("import_status")]
    public MiddlewareStatus ImportStatus { get; set; }
}

My azure function contains code to create a azure cosmos db document based on CustomBalanceTransaction class.

var customBalanceTransaction = new CustomBalanceTransaction
                        {
                            Id = balanceTransaction.Id,
                            Customerid = customer.Customerid,
                            ImportStatus = MiddlewareStatus.Received,
                            Source = balanceTransaction.Source,
                            Object = balanceTransaction.Object,
                            Amount = balanceTransaction.Amount,
                            AvailableOn = balanceTransaction.AvailableOn,
                            Created = balanceTransaction.Created,
                            Currency = balanceTransaction.Currency,
                            Description = balanceTransaction.Description,
                            ExchangeRate = balanceTransaction.ExchangeRate,
                            Fee = balanceTransaction.Fee,
                            FeeDetails = balanceTransaction.FeeDetails,
                            Net = balanceTransaction.Net,
                            ReportingCategory = balanceTransaction.ReportingCategory,
                            Status = balanceTransaction.Status,
                            Type = balanceTransaction.Type
                        };

                        var jsonString = JsonSerializer.Serialize(customBalanceTransaction);
                        var transaction = JsonSerializer.Deserialize<CustomBalanceTransaction>(jsonString);

                        log.LogInformation($"{jsonString}");

                        await transactionContainer.CreateItemAsync(transaction);```

When I serialise it to a string my json is correct and when I deserialise it, is correct. Whenever I insert it Azure cosmos DB it is missing my two properties.

json string in my console;
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/3RHXs.png

Document in azure cosmos db
[![enter image description here][2]][2]
  [2]: https://i.stack.imgur.com/6Po0v.png

CodePudding user response:

You are mixing Newtonsoft.Json and System.Text.Json in your model declaration.

The base type is using JsonProperty and your other type is using JsonPropertyName (System.Text.Json).

Since the Cosmos DB V3 SDK uses Newtonsoft.Json as serialization engine, try making your custom class use the same decorators:

public class CustomBalanceTransaction : BalanceTransaction
{
    [JsonProperty("customerid")]
    public string Customerid { get; set; }

    [JsonProperty("import_status")]
    public MiddlewareStatus ImportStatus { get; set; }
}
  • Related