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

ABP SystemTextJson inheritance response problem #6888

Open
totpero opened this issue Feb 13, 2024 · 0 comments
Open

ABP SystemTextJson inheritance response problem #6888

totpero opened this issue Feb 13, 2024 · 0 comments
Assignees
Milestone

Comments

@totpero
Copy link
Contributor

totpero commented Feb 13, 2024

Hi,
Related to this #6884 I have create different branch with this issue https://github.com/totpero/aspnetboilerplate-demo/tree/text-json
I have this demo classes structure:


    public class TestResutDto:EntityDto
    {
        public DateTime TestDate { get; set; }
        public IEnumerable<ISubItem> SubItems { get; set; }
    }

    public interface ISubItem
    {
        public int Prop1 { get; set; }
        public bool Prop2 { get; set; }
    }

    public class SubItemModel1 : ISubItem
    {
        public int Prop1 { get; set; }
        public bool Prop2 { get; set; }
        public int PropModel1Prop1 { get; set; }
        public bool PropModel1Prop2 { get; set; }
    }

    public class SubItemModel2 : ISubItem
    {
        public int Prop1 { get; set; }
        public bool Prop2 { get; set; }
        public int PropModel2Prop1 { get; set; }
        public bool PropModel2Prop2 { get; set; }
    }

And this call in my AppService method:

public TestResutDto TestMethod()
{
    var result = new TestResutDto
    {
        Id = 1,
        TestDate = DateTime.Now,
        SubItems = new ISubItem[] { 
            new SubItemModel1 { 
                Prop1 = 1,
                Prop2 = true, 
                PropModel1Prop1 = 100, 
                PropModel1Prop2 = false
            },
            new SubItemModel2 {
                Prop1 = 2,
                Prop2 = false,
                PropModel2Prop1 = 200,
                PropModel2Prop2 = true
            }
        }
    };
    return result;
}

I try to switch to Microsoft SystemTextJson in MVC app using this configurations:

.AddJsonOptions(options =>
	{
		options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
		options.JsonSerializerOptions.Converters.Add(new CultureInvariantDecimalJsonConverter());
		options.JsonSerializerOptions.Converters.Add(new CultureInvariantNullableDecimalJsonConverter());
		options.JsonSerializerOptions.Converters.Add(new CultureInvariantDoubleJsonConverter());
		options.JsonSerializerOptions.Converters.Add(new CultureInvariantNullableDoubleJsonConverter());

		options.JsonSerializerOptions.Converters.Add(new DateOnlyJsonConverter());

		options.JsonSerializerOptions.Converters.Add(new AbpNullableDateTimeConverter());
		options.JsonSerializerOptions.Converters.Add(new AbpDateTimeConverter());
	})

The output json result when I call this method in js is:

{
	"result": {
		"testDate": "2024-02-13T14:15:43.3535494+02:00",
		"subItems": [
			{
				"prop1": 1,
				"prop2": true
			},
			{
				"prop1": 2,
				"prop2": false
			}
		],
		"id": 1
	},
	"targetUrl": null,
	"success": true,
	"error": null,
	"unAuthorizedRequest": false,
	"__abp": true
}

There are missing the implementation class properties. The SystemTextJson return only the Intefrace properties.

If I switch to the old NewtonsoftJson configurations like this:


 .AddNewtonsoftJson(options =>
 {
     options.SerializerSettings.ContractResolver = new AbpCamelCasePropertyNamesContractResolver();
     options.SerializerSettings.Converters.Add(new CultureInvariantDecimalConverter());
     options.SerializerSettings.Converters.Add(new CultureInvariantDoubleConverter());

     options.SerializerSettings.Converters.Add(new DateOnlyJsonConverter());

 })

the json result is:

{
	"result": {
		"testDate": "2024-02-13T14:17:26.693521+02:00",
		"subItems": [
			{
				"prop1": 1,
				"prop2": true,
				"propModel1Prop1": 100,
				"propModel1Prop2": false
			},
			{
				"prop1": 2,
				"prop2": false,
				"propModel2Prop1": 200,
				"propModel2Prop2": true
			}
		],
		"id": 1
	},
	"targetUrl": null,
	"success": true,
	"error": null,
	"unAuthorizedRequest": false,
	"__abp": true
}

How to maintain the same behavior like NewtonsoftJson in Microsoft SystemTextJson migration?
Thanks

@ismcagdas ismcagdas added this to the v9.2 milestone Feb 14, 2024
@totpero totpero changed the title ABP SystemTextJson inheritance respose problem ABP SystemTextJson inheritance response problem Feb 18, 2024
@ismcagdas ismcagdas modified the milestones: v9.2, v9.3 Apr 24, 2024
@ismcagdas ismcagdas self-assigned this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants