Creating a derived type (not referenced by OData) and overriding a property changes the metadata (adds the un-referenced derived type and actually changes how the underlying property is exposed)
Create a class "MyDto" with a property for example "public DateTimeOffset TestProp { get; set; }"
Create a second class "MyDerivedDto : MyDto" with a property for example "public new string TestProp { <get/set point to the base property> }"
Issue 1: Metadata for the service now contains MyDerivedDto even when ONLY MyDto is referenced when setting up metadata via ODataConventionModelBuilder
Issue 2: Metadata for MyDto.TestProp has changed from DateTimeOffset to DateTimeOffset?
Suggest that when building up a type hierarchy, there is no need to go "up", only "down" to base and property types.
Hi, @nickkin-msft
1:
Convention model builder is designed to search all related types and their derived types from a starting point. So, If you only reference MyDto in convention model builder, it will include the derived type MyDerivedDto. It's by design.
So, builder.ComplexType<MyDto>(); will include both types.
However, builder.ComplexType<MyDerivedDto>(); will only include 1 types.
2:
For primitive type, for example DateTimeOffset, the nullable of the corresponding EDM property built by convention model builder depends on the CLR type. So,
public DateTimeOffset TestProperty {get;set;}
will result as:
<Property Name="TestProp" Type="Edm.DateTimeOffset" Nullable="false" />
Please pay attention to the attribute: Nullable="false"
While:
public DateTimeOffset? TestProperty {get;set;}
will result as:
<Property Name="TestProp" Type="Edm.DateTimeOffset" />
Let me show the my test result:
public class MyDto
{
public DateTimeOffset TestProp { get; set; }
}
public class MyDerivedDto : MyDto
{
public int DerivedProp { get; set; }
public new string TestProp { get; set; }
}
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.ComplexType<MyDto>();
return builder.GetEdmModel();
}
Then, I can get the following metadata document:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="ComplexTypeInheritanceTest" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<ComplexType Name="MyDto">
<Property Name="TestProp" Type="Edm.DateTimeOffset" Nullable="false" />
</ComplexType>
<ComplexType Name="MyDerivedDto" BaseType="ComplexTypeInheritanceTest.MyDto">
<Property Name="DerivedProp" Type="Edm.Int32" Nullable="false" />
</ComplexType>
</Schema>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="Container" />
</Schema>
</edmx:DataServices>
</edmx:Edmx>
MyDto cannot "see" MyDerivedDto. There is no reason for it to be present in the metadata, it is "dead metadata". MyDerivedDto is not anything that can ever be returned. You literally cannot implement a "[Queryable]public IQueryable
As for the nullable issue, that may be because in your test you created a second property in the derived type instead of overriding it with the "new" keyword as described in my original comment.
Seems when I typed IQueryable[LT]MyDerivedDto[GT] it caused GitHub to remove the type (also makes my comment not editable). Probably because it viewed it as HTML injection.
For # 2:
I updated the 'MyDerivedDto" class to add the new property, the result is same.
For # 1:
If you don't want the convention model builder to search for a certain type, you can call Igore() function. For example:
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.ComplexType<MyDto>();
builder.Ignore<MyDerivedDto>(); // Add this to ignore certain type.
return builder.GetEdmModel();
}
Then you can get the result:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="ComplexTypeInheritanceTest" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<ComplexType Name="MyDto">
<Property Name="TestProp" Type="Edm.DateTimeOffset" Nullable="false" />
</ComplexType>
</Schema>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="Container" />
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Besides, I notice that you are still using [IQueryable] attribute. I think it's already changed to [EnableQuery] attribute. Would you please tell me what version of OData Web API do you use?
Sorry, we are using EnableQuery, I was going by memory.
Sounds like using the Ignore() method would be a valid workaround.
Takeaway: I believe you should re-open this issue since the builder is putting types into metadata that should not be there and are literally impossible for the service to return. I tried it, and if you return the derived type on Get() then the service throws because a valid controller method can't be found. The derived stuff is just "dead metadata" that shouldn't be there. You should follow class inheritance downward and follow properties, but you shouldn't follow class inheritance "upward".
WRT # 2, I was looking at the results of the client code generator TT template. In this case it does make the value nullable when it shouldn't be. So that's an issue with the client generator not the server-side stuff in this repository. However, if you reopen this issue and fix the problem with the metadata (issue # 1) it appears that issue # 2 would automatically resolve itself.
@nickkin-msft
It is designed (built-in) in convention model builder to search and wrapper the derived types into metadata. This is the main functionality of convention model builder, and this is what most customers want using convention model builder. However, Web API OData also provides the other solution to do some custom configuration. for example, Ignore(). Besides, you can use *_non-convention model builder *_to do more exquisite configuration.
I reopen this issue to track your concerns. One of these is that you said you can't return the derived type on Get(). I think the problem is that Web API OData can't identify the TestProp from type.
Let me change the model to add a key for MyDto and create an entity set. for example:
public class MyDto
{
public int Id { get; set; }
public DateTimeOffset TestProp { get; set; }
}
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<MyDto>("MyDtos");
return builder.GetEdmModel();
}
Then, I can create the controller as:
public class MyDtosController : ODataController
{
private static IList<MyDto> MyDtos = new List<MyDto>
{
new MyDto { Id = 1, TestProp = new DateTimeOffset(2016, 2, 4, 11, 12, 13, 14, TimeSpan.Zero)},
new MyDerivedDto { Id = 2, TestProp = "DerivedTestProp", DerivedProp = 99}
};
[EnableQuery]
public IHttpActionResult Get()
{
return Ok(MyDtos);
}
[EnableQuery]
public IHttpActionResult Get(int key)
{
return Ok(MyDtos.FirstOrDefault(e => e.Id == key));
}
}
I will get the error message:
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.Reflection.AmbiguousMatchException: Ambiguous match found.
It's from the "TestProp" property serialization. It's obvious that "public PropertyInfo GetProperty(string name);" will return two "TestProp" properties and can't identify which one should return.
Maybe Web API OData can improve the function call.
Thank you for re-opening. I was able to find a different solution to my problem which didn't involve creating a derived type and overriding the property. In my case I never wanted to expose the derived type anyway, it was a workaround for a different issue and I didn't expect OData to pick it up in the first place. Given that you expect this to happen (picking up derived types), it seems I may have uncovered a different issue to be tracked than the one I originally opened this bug about.
In any case, as I said, thank you for re-opening. If you are able to solve this, either by saying "we won't pick up derived types" or "we've found a way to disambiguate" then it may help other consumers of your libs to avoid similar frustrations.
Close it. Please file a new issue if you have further concern. Thanks.