WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Schema Namespace missing from $metadata after update Microsoft.AspNet.OData 7.0.1 to 7.1.0

Open ImGonaRot opened this issue 7 years ago • 19 comments

Since the Namespace is missing, this broke the proxy code generators for connected services and the odata code generator. Without it, we can't determine what models belong to specific namespaces by using the metadata generated.

We use models in different Namespaces and those namespaces need to show in the service metadata

** Microsoft.AspNet.OData 7.0.1 **

<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="TestCompany.Service.Models.Reports">
    <EntityType Name="ReportType">
        <Key>
            <PropertyRef Name="ReportTypeID"/>
        </Key>
        <Property Name="ReportTypeID" Type="Edm.Int32" Nullable="false"/>
        <Property Name="Description" Type="Edm.String"/>
        <Property Name="Code" Type="Edm.String"/>
    </EntityType>
</Schema>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="TestCompany.Service.Models.Utilities">
    <EntityType Name="HttpLog">
        <Key>
            <PropertyRef Name="HttpLogID"/>
        </Key>
        <Property Name="HttpLogID" Type="Edm.Int32" Nullable="false"/>
        <Property Name="Url" Type="Edm.String" Nullable="false"/>
        <Property Name="ContentLength" Type="Edm.Int64"/>
        <Property Name="HttpStatusCode" Type="Edm.String"/>
        <Property Name="HttpMethod" Type="Edm.String"/>
        <Property Name="MessageDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
        <Property Name="CreateDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
    </EntityType>
</Schema>

** Microsoft.AspNet.OData 7.1.0 **

<EntityType Name="ReportType">
    <Key>
        <PropertyRef Name="ReportTypeID"/>
    </Key>
    <Property Name="ReportTypeID" Type="Edm.Int32" Nullable="false"/>
    <Property Name="Description" Type="Edm.String"/>
    <Property Name="Code" Type="Edm.String"/>
</EntityType>
<EntityType Name="HttpLog">
    <Key>
        <PropertyRef Name="HttpLogID"/>
    </Key>
    <Property Name="HttpLogID" Type="Edm.Int32" Nullable="false"/>
    <Property Name="Url" Type="Edm.String" Nullable="false"/>
    <Property Name="ContentLength" Type="Edm.Int64"/>
    <Property Name="HttpStatusCode" Type="Edm.String"/>
    <Property Name="HttpMethod" Type="Edm.String"/>
    <Property Name="MessageDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
    <Property Name="CreateDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
</EntityType>

** MODELS **

namespace TestCompany.Service.Models.Reports
{
    public partial class ReportType
    {
        [Key]
        public int ReportTypeID{ get; set; }
        public string Description { get; set; }
        public string Code { get; set; }
    }
}

namespace TestCompany.Service.Models.Utilities
{
    public partial class HttpLog
    {
        [Key]
        public int HttpLogID { get; set; }
        [Required]
        public string Url { get; set; }
        public Int64? ContentLength { get; set; }
        public string HttpStatusCode { get; set; }
        public string HttpMethod { get; set; }
        public DateTime MessageDateTime { get; set; }
        public DateTime CreateDateTime { get; set; }
    }
}

ImGonaRot avatar Feb 26 '19 21:02 ImGonaRot

@ImGonaRot It seems this problem is related to https://github.com/OData/WebApi/commit/de7dcd076385664768841856d3a1db1b7ce96b30 @RamjotSingh Do you have any hints?

xuzhg avatar Feb 27 '19 18:02 xuzhg

I am not able to repro this. Here is the code I wrote and used your models. TestOData.zip

And this is the metadata I got out of it

<?xml version="1.0" encoding="ISO-8859-1"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <edmx:DataServices>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="TestCompany.Service.Models.Reports">
            <EntityType Name="ReportType">
                <Key>
                    <PropertyRef Name="ReportTypeID"/>
                </Key>
                <Property Name="ReportTypeID" Nullable="false" Type="Edm.Int32"/>
                <Property Name="Description" Type="Edm.String"/>
                <Property Name="Code" Type="Edm.String"/>
            </EntityType>
        </Schema>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="TestCompany.Service.Models.Utilities">
            <EntityType Name="HttpLog">
                <Key>
                    <PropertyRef Name="HttpLogID"/>
                </Key>
                <Property Name="HttpLogID" Nullable="false" Type="Edm.Int32"/>
                <Property Name="Url" Nullable="false" Type="Edm.String"/>
                <Property Name="ContentLength" Type="Edm.Int64"/>
                <Property Name="HttpStatusCode" Type="Edm.String"/>
                <Property Name="HttpMethod" Type="Edm.String"/>
                <Property Name="MessageDateTime" Nullable="false" Type="Edm.DateTimeOffset"/>
                <Property Name="CreateDateTime" Nullable="false" Type="Edm.DateTimeOffset"/>
            </EntityType>
        </Schema>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
            <EntityContainer Name="Container"/>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

Would you be able to isolate your code into a smaller project and attach it here so I can take a look?

RamjotSingh avatar Mar 01 '19 04:03 RamjotSingh

Alright I followed a hunch and wanna check if this is the case for you. Are you declaring all the models with DataContract notation and then making the namespace empty on them? Something like

[DataContract(Namespace = "")]

In this case I see the namespace gets empty and that is working as intended. Anything specified in DataContract takes precedence.

RamjotSingh avatar Mar 01 '19 04:03 RamjotSingh

@RamjotSingh I am not using any DataContract attributes on the models. The models are about 99.9% exact as pasted above.

I will work on isolating this into a sample project and post it here once I have that finished. It is really odd in that all the "Schema" attributes have been removed from the metadata data other than the one at the very beginning of the metadata.

ImGonaRot avatar Mar 01 '19 04:03 ImGonaRot

Are you setting the namespace in the builder? Something like

var builder = new ODataConventionModelBuilder()
{
    Namespace = "TestNamespace"
};

If yes then I think this is expected. Your elements have been moved to that namespace (this was actually the original bug). Earlier Namespace was not being honored when setting on Models. It was just getting honored on Actions and Functions. 7.1.0 fixed this to make sure Namespace is honored everywhere. In this case if all your models have the same namespace and are clubbed under the same Schema element. Something like

<?xml version="1.0" encoding="ISO-8859-1"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <edmx:DataServices>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="TestNamespace">
            <EntityType Name="ReportType">
                <Key>
                    <Property Name="ReportTypeID" Nullable="false" Type="Edm.Int32"/>
                    <Property Name="Description" Type="Edm.String"/>
                    <Property Name="Code" Type="Edm.String"/>
                </EntityType>
                <EntityType Name="HttpLog">
                    <Key>
                        <PropertyRef Name="HttpLogID"/>
                    </Key>
                    <Property Name="HttpLogID" Nullable="false" Type="Edm.Int32"/>
                    <Property Name="Url" Nullable="false" Type="Edm.String"/>
                    <Property Name="ContentLength" Type="Edm.Int64"/>
                    <Property Name="HttpStatusCode" Type="Edm.String"/>
                    <Property Name="HttpMethod" Type="Edm.String"/>
                    <Property Name="MessageDateTime" Nullable="false" Type="Edm.DateTimeOffset"/>
                    <Property Name="CreateDateTime" Nullable="false" Type="Edm.DateTimeOffset"/>
                </EntityType>
                <EntityContainer Name="Container"/>
            </Schema>
        </edmx:DataServices>
    </edmx:Edmx>

Is this what you are seeing? If yes, then this is by design and OData convention.

At this point if you want to move them to a different namespace you can use DataContract on the classes to override and move them into separate namespaces. One example is below

    [DataContract(Namespace = "MyNamespace")]
    public partial class HttpLog
    {
        [Key]
        [DataMember]
        public int HttpLogID { get; set; }

        [Required]
        [DataMember]
        public string Url { get; set; }

        [DataMember]
        public Int64? ContentLength { get; set; }

        [DataMember]
        public string HttpStatusCode { get; set; }

        [DataMember]
        public string HttpMethod { get; set; }

        [DataMember]
        public DateTime MessageDateTime { get; set; }

        [DataMember]
        public DateTime CreateDateTime { get; set; }
    }

RamjotSingh avatar Mar 01 '19 07:03 RamjotSingh

@RamjotSingh That is exactly what caused the problem. I had the following in my ModelBuilder

builder.Namespace = "TestCompany.Service";

After removing this line from the ModelBuilder, I can see that the Schema attributes are back in the metadata for the models BUT.... there was a reason I had added that into the WebApiConfig This now broke the Schema attribute for "Function / Action" in specific namespaces.

Back before I had added builder.Namespace = "TestCompany.Service"; all my functions we being placed in the "Default" namespace even though they belong to specific namespaces.

So in Microsoft.AspNet.OData 7.0.1 my metadata for functions looked like this.

<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="TestCompany.Service">
<Function Name="TestFunction" IsBound="true">
<Parameter Name="bindingParameter" Type="TestCompany.Service.Models.TestObject"/>
<ReturnType Type="Edm.String" Unicode="false"/>
</Function>

<ActionImport Name="TestAction" Action="TestCompany.Service.TestAction"/>
<FunctionImport Name="AnotherTestFunction" Function="TestCompany.Service.AnotherTestFunction" IncludeInServiceDocument="true"/>

BUT now with Microsoft.AspNet.OData 7.1.0 it is back to "default" which does not help us separate functions / action to specific namespaces / models.

<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
<Function Name="TestFunction" IsBound="true">
<Parameter Name="bindingParameter" Type="TestCompany.Service.Models.TestObject"/>
<ReturnType Type="Edm.String"/>
</Function>

<ActionImport Name="AnotherTestFunction" Action="Default.AnotherTestFunction"/>
<FunctionImport Name="AnotherTestFunction" Function="Default.AnotherTestFunction" IncludeInServiceDocument="true"/>

This stems all back to https://github.com/OData/WebApi/issues/1223 in that OData does not allow functions / action to be bound to specific namespaces.

The "desired" metadata for Function / Action would be to include the namespace that the Function / Action is bound to.

Lets assume you have the following

namespace TestCompany.Service.Controllers.Reports
{
    public class ObjectsController : ODataController
    {
        [HttpGet]
        public IHttpActionResult SomeTest([FromODataUri] int key)
        {
            return Ok("TEST");
        }
    }
}

namespace TestCompany.Service.Controllers.Utilities
{
    public class ObjectsController : ODataController
    {
        [HttpGet]
        public IHttpActionResult SomeOtherTest([FromODataUri] int key)
        {
            return Ok("TEST");
        }
    }
}

Notice that both these controllers are in 2 different namespaces. Yet OData does not support the above in that it puts "ALL" functions / actions in the "Default" namespace. So I added the builder.Namespace = "TestCompany.Service"; a while back to try to get around this problem.

ALSO... I just noticed that the metadata is still wrong even if I remove builder.Namespace = "TestCompany.Service"; Removing this line DOES add back the Schema tags to the TOP of the metadata file but at the BOTTOM it pushes the "Container" into the "Default" namespace which was not there before and this is causing some issues on client proxy generators.

** Microsoft.AspNet.OData 7.0.1 **

<Function Name="Test">
<ReturnType Type="Edm.String" Unicode="false"/>
</Function>
<EntityContainer Name="Container">
<EntitySet Name="Companies" EntityType="TestCompany.Service.Models.Reports.Company">

** Microsoft.AspNet.OData 7.1.0 ** NOTICE: There is a "Default" schema that was not there before...

<Function Name="Test">
<ReturnType Type="Edm.String"/>
</Function>
</Schema>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
<EntityContainer Name="Container">
<EntitySet Name="Companies" EntityType="TestCompany.Service.Models.Reports.Company">

ImGonaRot avatar Mar 01 '19 14:03 ImGonaRot

Namespace not getting used in Structural and Enum types was a bug and the commit above fixed that bug. However I understand you are having difficulty fixing these. You have a couple of options here.

Adding namespace to functions directly

You can directly put functions in namespaces. This lets you pull the functions out of the default namespace into a special one.

 builder.Function("func").Returns(typeof(HttpLog)).Namespace = "myNamespace";

Keeping all functions, actions and container in one namespace and models in another

In this case you need to set the namespace at ODataConventionModelBuilder level and then to put individual models in different namespaces you have following options

Set namespace using DataContract attribute

You can use DataContract to override the namespace

[DataContract(Namespace = "MyNamespace")]
    public partial class HttpLog
Set namespace during registration
builder.EntityType<ReportType>().Namespace = "NewNamespace";

These are the options you can work with to put things in the correct namespaces. If this still does not resolve your issue let me know exactly how you want your things. Which model in which namespaces, which function or action where and I can write the model builder code for you to generate it that way.

RamjotSingh avatar Mar 01 '19 17:03 RamjotSingh

@RamjotSingh Sorry guess I should have clarified a little better. I have added "Namespace" to all FunctionConfiguration and ActionConfiguration in the WebApiConfig before posting to "correct" the removal from Microsoft.AspNet.OData 7.1.0 and that did indeed add the namespace I needed on all the Functions / Actions BUT... The problem I am facing now is that the "Container" is in the "Default" namespace when it should be in the "TestCompany" namespace.

How do I get the "Container" to appear in the "TestCompany" namespace like it did in Microsoft.AspNet.OData 7.0.1 ? You can see a snippet of the XML listed in the previous post.

ImGonaRot avatar Mar 01 '19 17:03 ImGonaRot

Just set the namespace at model builder level (yes that will make you to move all your models using direct namespace setting or DataContract).

RamjotSingh avatar Mar 01 '19 17:03 RamjotSingh

@RamjotSingh Now we are full circle. If I set the Namespace in the model builder then it removes the Schema attribute from all the models in the metadata.

Unless you are suggesting that I have to set the Namespace in the model builder AND use the DataContract on each model. If so, then that is a mess going forward. I'm not sure why I didn't have to add DataContract to all the models and now I have to add it. The metadata builder should be able to get the namespace from the model rather than me declaring it on every model.

ImGonaRot avatar Mar 01 '19 18:03 ImGonaRot

It is not removing Schema attribute but moving them all to same namespace. Model generator does honor namespace of the models but only when you have not asked it to use the namespace you specified.

At this point for your case DataContract is the best approach. However you have another option.

  • Do not set the namespace at model builder level
  • Set namespace individually for every function\action
  • Set the container namespace during OnModelCreating phase something like
            builder.OnModelCreating = (modelBuilder) =>
            {
                modelBuilder.Namespace = "MyNewNamespace";
            };

I would not recommend this approach. But this lets the models take their namespace as the final namespace and then change the namespace of the container at last possible moment to shift it where you want.

RamjotSingh avatar Mar 01 '19 18:03 RamjotSingh

@RamjotSingh Thanks for all your input but it does not seem to work for me as you are describing. Let me create a sample project this weekend and I will post here to better explain the problem.

ImGonaRot avatar Mar 01 '19 18:03 ImGonaRot

Sure. Just give me the sample project and tell me what you want in which namespace and I can set it up for you.

RamjotSingh avatar Mar 01 '19 18:03 RamjotSingh

Just curious what did not work in the above sample for?

RamjotSingh avatar Mar 01 '19 18:03 RamjotSingh

Also to be clear. I am not having problems with the namespace on the model, functions, or actions now that you have sent me in the right direction. My problem is that the "Container" is now under "Default" when it use to be under "TestCompany".

The sample project you provided is very limited in that it does not split controllers or entities into separate namespaces and I will have to generate a sample project to explain further.

ImGonaRot avatar Mar 01 '19 18:03 ImGonaRot

            builder.OnModelCreating = (modelBuilder) =>
            {
                modelBuilder.Namespace = "TestCompany";
            };

Should fix that.

RamjotSingh avatar Mar 01 '19 18:03 RamjotSingh

Sample project I created was based on the info you provided. If this does not work for you, I will wait for your sample.

RamjotSingh avatar Mar 01 '19 18:03 RamjotSingh

For what it is worth I followed all the steps here and ended up in the same position as ImGonaRot.

image

I finally decided to bite the bullet and refactor all the code using the OData generated clients. It was a ton of work.

RetrotecEd avatar Aug 07 '19 20:08 RetrotecEd

Not sure what has changed but all is working fine with Microsoft.AspNet.OData 7.3.0 I think changing this builder.Namespace = "TestCompany"; To this (like you said before) is what really fixed it.

builder.OnModelCreating = (modelBuilder) =>
{
    modelBuilder.Namespace = "TestCompany";
};

ImGonaRot avatar Feb 23 '20 02:02 ImGonaRot