Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for JsonPropertyName and other Attributes #2507

Closed
dgrozenok opened this issue Feb 28, 2023 · 2 comments · Fixed by #2513
Closed

Support for JsonPropertyName and other Attributes #2507

dgrozenok opened this issue Feb 28, 2023 · 2 comments · Fixed by #2513

Comments

@dgrozenok
Copy link

I understand that Marten doesn't support the Json serialization attributes on properties except the [JsonIgnore] one. Here is the use case I'm struggling to finding a workaround though.

I use an external web api which provides the OpenAPI schema. For example, something like this.

In order to access the web api I use NSwag.CodeGeneration.CSharp nuget package or any other NSwag tools for generating the C# classes. I tried different settings, but NSwag always adds the JsonPropertyName attribute to every property to match the OpenAPI specification. I don't want to reimplement all those records manually and I would like to be able to store the object to Marten. It is stored correctly, but when I use LINQ to filter or sort by a property the C# property name and casing is used in the SQL which obviously is not matched to the Json. Would it be possible while generating the SQL instead of just using the property name to try to pull the [JsonPropertyName] attribute for that property and use it if it's specified? Here is an example of the code generated by NSwag. Please let me know, if there is any workaround available in the short-term, besides just manually creating the classes?

    /// <summary>
    /// This type is used by the &lt;strong&gt;taxIdentifier&lt;/strong&gt; container that is returned in &lt;strong&gt;getOrder&lt;/strong&gt;. The &lt;strong&gt;taxIdentifier&lt;/strong&gt; container consists of taxpayer identification information for buyers from Italy, Spain, or Guatemala. It is currently only returned for orders occurring on the eBay Italy or eBay Spain marketplaces.&lt;br&gt;&lt;br&gt;&lt;span class="tablenote"&gt;&lt;strong&gt;Note:&lt;/strong&gt; Currently, the &lt;strong&gt;taxIdentifier&lt;/strong&gt; container is only returned in &lt;strong&gt;getOrder&lt;/strong&gt; and not in &lt;strong&gt;getOrders&lt;/strong&gt;. So, if a seller wanted to view a buyer's tax information for a particular order returned in &lt;strong&gt;getOrders&lt;/strong&gt;, that seller would need to use the &lt;strong&gt;orderId&lt;/strong&gt; value for that particular order, and then run a &lt;strong&gt;getOrder&lt;/strong&gt; call against that order ID. &lt;/span&gt;
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v10.0.0.0))")]
    public partial record TaxIdentifier
    {
        [System.Text.Json.Serialization.JsonConstructor]
        public TaxIdentifier(string @issuingCountry, string @taxIdentifierType, string @taxpayerId)
        {
            this.TaxpayerId = @taxpayerId;
            this.TaxIdentifierType = @taxIdentifierType;
            this.IssuingCountry = @issuingCountry;
        }    

        /// <summary>
        /// This value is the unique tax ID associated with the buyer. The type of tax identification is shown in the &lt;strong&gt;taxIdentifierType&lt;/strong&gt; field.
        /// </summary>
        [System.Text.Json.Serialization.JsonPropertyName("taxpayerId")]
        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]   
        public string TaxpayerId { get; init; }

        /// <summary>
        /// This enumeration value indicates the type of tax identification being used for the buyer. The different tax types are defined in the &lt;strong&gt;TaxIdentifierTypeEnum&lt;/strong&gt; type. For implementation help, refer to &lt;a href='https://developer.ebay.com/api-docs/sell/fulfillment/types/sel:TaxIdentifierTypeEnum'&gt;eBay API documentation&lt;/a&gt;
        /// </summary>
        [System.Text.Json.Serialization.JsonPropertyName("taxIdentifierType")]
        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]   
        public string TaxIdentifierType { get; init; }

        /// <summary>
        /// This two-letter code indicates the country that issued the buyer's tax ID. The country that the two-letter code represents can be found in the &lt;strong&gt;CountryCodeEnum&lt;/strong&gt; type, or in the  &lt;a href="https://www.iso.org/iso-3166-country-codes.html "&gt;ISO 3166&lt;/a&gt; standard. For implementation help, refer to &lt;a href='https://developer.ebay.com/api-docs/sell/fulfillment/types/ba:CountryCodeEnum'&gt;eBay API documentation&lt;/a&gt;
        /// </summary>
        [System.Text.Json.Serialization.JsonPropertyName("issuingCountry")]
        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]   
        public string IssuingCountry { get; init; }

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

        [System.Text.Json.Serialization.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
            set { _additionalProperties = value; }
        }
    }
@jeremydmiller
Copy link
Member

Let's just start with STJ [JsonPropertyName] and the Newtonsoft [JsonProperty] attributes :-)

@dgrozenok
Copy link
Author

Great! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants