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

DerivesFrom not work on 7.9.0 #2092

Closed
premiaware opened this issue May 17, 2021 · 6 comments
Closed

DerivesFrom not work on 7.9.0 #2092

premiaware opened this issue May 17, 2021 · 6 comments
Assignees

Comments

@premiaware
Copy link

premiaware commented May 17, 2021

I have this code on

public static void Register(HttpConfiguration config)
{
......
            builderWeb.EntitySet<Regioni>("Regioni");
            builderWeb.EntitySet<Uffici>("Uffici");
            builderWeb.EntitySet<UfficiExt>("UfficiExt");
            builderWeb.EntityType<UfficiExt>().DerivesFrom<Uffici>();
......
}

On the controller i have this method

 [HttpGet]
        public async Task<IHttpActionResult> Ufficio([FromODataUri] Guid key)
        {
            UfficiExt ufficioExt = new UfficiExt();
........
........

            return Ok<UfficiExt>(ufficioExt);
        }

With version 7.8.3 all working Ok!!!

With version 7.9.0 the error is:

{"error":{"code":"","message":"An error has occurred.","innererror":{"message":"The 'ObjectContent1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{"message":"The type 'FHServices.Models.UfficiExt' does not inherit from and is not a base type of 'FHServices.Models.LandingPageImmobili'. The type of 'KeySegments' must be related to the Type of the EntitySet.","type":"Microsoft.OData.ODataException","stacktrace":" in Microsoft.OData.UriParser.ExceptionUtil.ThrowIfTypesUnrelated(IEdmType type, IEdmType secondType, String segmentName)\r\n in Microsoft.OData.UriParser.KeySegment..ctor(IEnumerable1 keys, IEdmEntityType edmType, IEdmNavigationSource navigationSource)\r\n in Microsoft.OData.UriParser.ODataPathExtensions.AddKeySegment(ODataPath path, IEnumerable1 keys, IEdmEntityType edmType, IEdmNavigationSource navigationSource)\r\n in Microsoft.OData.ODataWriterCore.AppendEntitySetKeySegment(ODataPath odataPath, Boolean throwIfFail)\r\n in Microsoft.OData.ODataWriterCore.EnterScope(WriterState newState, ODataItem item)\r\n in Microsoft.OData.ODataWriterCore.WriteStartNestedResourceInfoImplementation(ODataNestedResourceInfo nestedResourceInfo)\r\n in Microsoft.OData.ODataWriterCore.WriteStart(ODataNestedResourceInfo nestedResourceInfo)\r\n in Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteComplexProperties(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)\r\n in Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteResource(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)\r\n in Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)\r\n in Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\r\n in Microsoft.AspNet.OData.Formatter.ODataOutputFormatterHelper.WriteToStream(Type type, Object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, IWebApiUrlHelper internaUrlHelper, IWebApiRequestMessage internalRequest, IWebApiHeaders internalRequestHeaders, Func2 getODataMessageWrapper, Func2 getEdmTypeSerializer, Func2 getODataPayloadSerializer, Func`1 getODataSerializerContext)\r\n in Microsoft.AspNet.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- Fine traccia dello stack da posizione precedente dove \u00e8 stata generata l'eccezione ---\r\n in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n in System.Web.Http.WebHost.HttpControllerHandler.d__22.MoveNext()"}}}}

@Sreejithpin
Copy link
Contributor

Sreejithpin commented May 18, 2021

Hi,
It would be really great if you can please share a repro. Also can you please share the model (probably as a part of repro) and the request you are making which causes the exception
thanks

@Sreejithpin
Copy link
Contributor

@premiaware , Will be really great if you can please provide the same (repro and request) and we can fix it
Thanks

@gathogojr
Copy link
Contributor

@premiaware This line in the stack trace piqued my interest:

'FHServices.Models.UfficiExt' does not inherit from and is not a base type of 'FHServices.Models.LandingPageImmobili'

Why does it refer to FHServices.Models.LandingPageImmobili?

The sample code you shared above suggests the target framework for your application is NET Framework 4.*
AFAIK, if you installed the latest Microsoft.AspNet.OData nuget package (7.5.8) in your project, the version of ODL library that is installed is 7.8.2. Did you manually update the ODL library package on your project to 7.9.0?

I tried that and everything works fine with the following application code:
Packages:

  • Microsoft.AspNet.OData 7.5.8
  • Microsoft.OData.Core 7.9.0
  • Microsoft.OData.Edm 7.9.0
  • Microsoft.Spatial 7.9.0
// Models
public class Animal
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Dog : Animal
{
    public string Breed { get; set; }
}

// Service configuration
public static void Register(HttpConfiguration config)
{
    var modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.EntitySet<Animal>("Animal");
    modelBuilder.EntitySet<Dog>("Dogs");
    modelBuilder.EntityType<Dog>().DerivesFrom<Animal>();

    config.MapODataServiceRoute("odata", "odata", modelBuilder.GetEdmModel());
    config.Select().Filter().Expand().OrderBy().Count().MaxTop(null);
    config.EnsureInitialized();
}

// Controller
public class DogsController : ODataController
{
    private readonly IList<Dog> dogs = new List<Dog>
    {
        new Dog { Id = 1, Name = "Butch", Breed = "Pitbull" }
    };

    public IHttpActionResult Get([FromODataUri] int key)
    {
        var dog = dogs.SingleOrDefault(d => d.Id.Equals(key));

        if (dog == null)
        {
            return NotFound();
        }

        return Ok<Dog>(dog);
    }
}

Request:

http://localhost/odata/Dogs(1)

Response

{"@odata.context":"http://localhost/odata/$metadata#Dogs/$entity","Id":1,"Name":"Butch","Breed":"Pitbull"}

@premiaware
Copy link
Author

Why does it refer to FHServices.Models.LandingPageImmobili?
I don't Know! i have try with sample but it working, in my production project not work.

my class is

public class TelefonoDescrittivo
{
public string Etichetta { get; set; }
public string Numero { get; set; }
}

public partial class UfficiExt : Uffici
{
    public List<TelefonoDescrittivo> Telefoni { get; set; }
}

Working only i change the
public List Telefoni { get; set; } ---> public List Telefoni { get; set; }

or i declare TelefonoDescrittivo With Entity.

Now i have downgrade the version and working well.

@Sreejithpin
Copy link
Contributor

Thanks @premiaware , Will be great if you can share a repro, your model and the request you are making so that we can get the exact scenario

@premiaware
Copy link
Author

I have update my odata project to .net 6 using odata 8.0.12 and the previous issue return.
this is the thread https://github.com/OData/AspNetCoreOData/issues/760
Using odata 7.12.5 all is ok!!!
for my point of view this is a issue with bounded function controller that return data type that containt complex type. if change the complex type
public List<TelefonoDescrittivo> Telefoni { get; set; }

to
public List<string> Telefoni { get; set; }

all working well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants