question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to bind a model with LocalDate property

See original GitHub issue

If a model has a property LocalDate (Noda Time), Bind<T> silently creates a new instance of T with all properties initialized with their default values.

Reproduced with DefaultJsonModelBinder, but not NewtonsoftJsonModelBinder

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Deilancommented, Mar 3, 2021

@ritasker Thank you very much for your help! You are right.

For some reason even just switching to Carter’s NewtonsoftJsonResponseNegotiator and NewtonsoftJsonModelBinder was enough:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCarter(configurator: configurator =>
    {
        configurator.WithResponseNegotiator<NewtonsoftJsonResponseNegotiator>();
        configurator.WithModelBinder<NewtonsoftJsonModelBinder>();
    });
}
0reactions
jnysteencommented, Sep 24, 2022

This might help. It might not. https://stackoverflow.com/questions/69850917/how-to-configure-newtonsoftjson-with-minimalapi-in-net-6-0

Thanks, that link led me down the right path. The JsonOptions used when deserialising request contents can now be configured to work for NodaTime types like this:

...
using Microsoft.AspNetCore.Http.Json;
using NodaTime.Serialization.SystemTextJson;
...

services.AddCarter();
services.Configure<JsonOptions>(opt => opt.SerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb));

This blog post deserves a link and a quote:

Minimal APIs rely on a number of extension methods for serializing to/from JSON. They allow for JsonSerializerOptions to be provided but otherwise falls back to retrieving JsonOptions from HttContext.Request.Services. You can configure these options at startup:

builder.Services.Configure<JsonOptions>(opt =>
{
    opt.SerializerOptions.PropertyNamingPolicy = new SnakeCaseNamingPolicy());
});

Note that you need to configure Microsoft.AspNetCore.Http.Json.JsonOptions not the class under the Mvc namespace.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to LocalDate Property on ModelBind with Json.Net ...
I have a model class with a single property in it. The property is of type "LocalDate". No matter, what I do the...
Read more >
Fail to binding LocalDate without @ModelAttribute
I added @DateTimeFormat for binding String argument to LocalDate, but without @ModelAttribute the variable startDate did not binded and was null ...
Read more >
Property Binding in Spring Boot 2.0
Since the first release of Spring Boot, it has been possible to bind properties to classes by using the @ConfigurationProperties annotation.
Read more >
How to persist LocalDate and LocalDateTime with JPA 2.1
The LocalDate and LocalDateTime classes are not supported by JPA and Hibernate. This can be changed by implementing an attribute converter for them....
Read more >
Working with Date Parameters in Spring
In this short tutorial, we'll learn how to accept Date, LocalDate, and LocalDateTime parameters in Spring REST requests, both at the request ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found