-
Notifications
You must be signed in to change notification settings - Fork 704
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
Error in EDM model when using ODataApiExplorer with JSON columns #1113
Comments
This might be a gap in the implementation. I can tell by what you've shared that you are in the Partial OData camp. You're not really using OData the protocol. You're just taking advantage of the query options. There's nothing wrong with that per se and why reinvent a HTTP-based query language. This path, however, does come with some quirks and limitations. First and foremost, the API Explorer extensions for OData are based on an EDM. EF and OData both use EDM, but not the same EDM model or instance. The EDM for EF is about mapping your model to a database. The OData EDM is for mapping your model over the wire (e.g. HTTP). You don't have an OData EDM (that I see). That is to be expected in this scenario. Since the API Explorer extensions only know how to operate on an OData EDM, it generates one on the fly. It appears you've encountered a sharp edge where:
I'm almost certain it's The reason you haven't seen this before is that you aren't actually using OData in your controller. That requires applying I think with a little bit of configuration help on your side, you'll achieve the result you're looking for. |
Thanks to your guidance I have been able to make some progress on this. I have run into a related issue and I am unsure if it is my lack of EDM model knowledge, if I am implementing the versioning incorrectly, or if it is actually a bug. To configure the model to solve the original issue, I have added a model configuration: public class CustomerModelConfiguration : IModelConfiguration
{
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion, string? routePrefix)
{
_ = builder.EntitySet<Customer>("Customers");
_ = builder.ComplexType<Address>();
}
} This works fine but in my production code the entity causing the issue is only in version 2 of the API so I added a check against the API version to only configure the model for version 2: public class CustomerModelConfiguration : IModelConfiguration
{
private static readonly ApiVersion s_version1 = new(1, 0);
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion, string? routePrefix)
{
if (apiVersion > s_version1)
{
_ = builder.EntitySet<Customer>("Customers");
_ = builder.ComplexType<Address>();
}
}
} With this check in place the exception will still occur when processing version 1. This error appears to short circuit the configuration process and version 2 is never configured. I have tried casting the builder to |
Hmm... This might actually be a bug. The generated EDM is only created and used by API Explorer. It's actually ephemeral. The workaround should be to use the same model configuration for every API version. I'll have to dig deeper as to why it still causes a problem. A distinct EDM is created for each API version. I wouldn't expect an explicit |
Is there an existing issue for this?
Describe the bug
I recently configured JSON columns in a service using Microsoft.EntityFrameworkCore.SqlServer 8.0.10 and this caused Swashbuckle.AspNetCore 6.8.1 to fail. Investigating further it looks like this might be caused by the EDM model created by Asp.Versioning.OData.ApiExplorer 8.1.0 failing validation. It seems like the EDM model is expecting the owned entities from the JSON to have a key but in my case they are more like value objects. The exception only causes Swashbuckle to fail. I can still call the endpoint and get a response using methods other than the Swagger endpoint.
Expected Behavior
No response
Steps To Reproduce
Create an entity that owns a list of other entities:
Configure a
DbContext
that persists the list in database as JSON:Create a controller for the entity:
Wire it all up in Program.cs:
Exceptions (if any)
.NET Version
8.0.403
Anything else?
No response
The text was updated successfully, but these errors were encountered: