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

AbpJsonOptions DefaultDateTimeFormat not applied #12604

Closed
Ultre00 opened this issue May 16, 2022 · 4 comments
Closed

AbpJsonOptions DefaultDateTimeFormat not applied #12604

Ultre00 opened this issue May 16, 2022 · 4 comments

Comments

@Ultre00
Copy link

Ultre00 commented May 16, 2022

  • Version 5.2.1

Created a a new ABP project with the cli. In the HttpApiHost Module I added this:

public override void PreConfigureServices(ServiceConfigurationContext context)
{
    PreConfigure<AbpJsonOptions>(options =>
    {
        options.UseHybridSerializer = false;
        options.DefaultDateTimeFormat = "yyyy/MM/dd HH:mm:ss";
    });
}

Added a simple application service:

public class DemoDto
{
    public DateTime DateTest { get; set; }
}

public class DemoAppService : BookStoreAppService
{        
        
    public DemoDto GetDemo()
    {
        return new DemoDto()
        {
            DateTest = DateTime.UtcNow
        };
    } 
}

When testing the endpoint the result was:

{
  "dateTest": "2022-05-16T08:27:48.6620871Z"
}

How can I get the datetime to serialize as the provided AbpJsonOptions.DefaultDateTimeFormat ?

The actual issue I am facing in my original project is that the 'Z' is missing in the end. So when a datetime is serialized it shows as 2022-05-12T22:22:08.8273489 instead of 2022-05-12T22:22:08.8273489Z

@maliming
Copy link
Member

The actual issue I am facing in my original project is that the 'Z' is missing in the end. So when a datetime is serialized it shows as 2022-05-12T22:22:08.8273489 instead of 2022-05-12T22:22:08.8273489Z

This is a typical XY problem, please describe your original problem in detail.

https://xyproblem.info/

@Ultre00
Copy link
Author

Ultre00 commented May 16, 2022

@maliming The original problem is that an entity has a DateTime property. I am using Entity Framework as the database provider. When the entity is retrieved from the database the DateTime kind is set to unspecified. Because of this the serializer is not adding the 'Z' in the end because it doesn't know it is a UTC kind. That's why I wanted to specify the format myself since all the datetimes are always UTC.

I am also configuring the clock:

Configure<AbpClockOptions>(options => options.Kind = DateTimeKind.Utc); 

@maliming
Copy link
Member

Our framework has unit tests ensure that the problem you mentioned do not happen.

Can you reproduce it with unit tests or a template project?

@Ultre00
Copy link
Author

Ultre00 commented May 17, 2022

After a lot of debugging I found the issue. It was because I am using a custom serializer that I forgot about. This serializer is set as an attribute on the Dto and it is only used in inheritance so the discriminator property is automatically serialized. However because this is a custom serializer the other serializers are not being applied anymore 🤦‍♂️

@Ultre00 Ultre00 closed this as completed May 17, 2022
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