Code Monkey home page Code Monkey logo

Comments (6)

julealgon avatar julealgon commented on June 16, 2024

Check if it makes any difference if you remove the Ok from your action implementation:

Switch from this:

	public ActionResult< IQueryable > Get(
		ODataQueryOptions< Customer > queryOptions )
	{
		IQueryable< Customer > values = s_Customers.AsQueryable( );

		return Ok(
			queryOptions.SelectExpand.ApplyTo(
				values,
				new ODataQuerySettings( ) ) );
	}

To this:

	public ActionResult< IQueryable > Get(
		ODataQueryOptions< Customer > queryOptions )
	{
		IQueryable< Customer > values = s_Customers.AsQueryable( );

		return queryOptions.SelectExpand.ApplyTo(
			values,
			new ODataQuerySettings( ) ) ;
	}

from aspnetcoreodata.

KnapSac avatar KnapSac commented on June 16, 2024

Removing the Ok doesn't compile, if I change the return type to IQueryable it does, but the return value is missing the @odata.context property.

Without wrapping return type of Get in ActionResult:

[{"Id":1},{"Id":2},{"Id":3}]

When wrapping return type of Get with ActionResult and selecting name too:

{"@odata.context":"http://localhost:5247/$metadata#Customers","value":[{"Id":1,"Name":"Customer 1"},{"Id":2,"Name":"Customer 2"},{"Id":3,"Name":"Customer 3"}]}

from aspnetcoreodata.

julealgon avatar julealgon commented on June 16, 2024

@KnapSac I tried some different things with your sample and I believe this is the correct way to approach your problem if you only want to apply the select portion from ODataQueryOptions:

	public ActionResult<IQueryable> Get(
		ODataQueryOptions<Customer> queryOptions)
	{
		IQueryable values = s_Customers.AsQueryable();

		values = queryOptions.ApplyTo(values, ignoreQueryOptions: ~AllowedQueryOptions.Select);

		return Ok(values);
	}

Instead of attempting to explicitly apply only the SelectExpand property, you apply the whole ODataQueryOptions object and specify which operations you want to exclude. In this case, we "exclude everything but $select" by passing the negated select option.

However.... when I test this, for whatever reason, I noticed that even if I pass something else on the ignoreQueryOptions, the actual select operation still happens. The only difference I see is that when $select is "blocked", it doesn't show in the @odata.context path.

I don't understand what is going on here... it's as if the [EnableQuery] attribute is somehow active on the action, even though it is not specified. But then... if I remove the ApplyTo call completely and just return the original datasource, it does NOT perform the selection at all.

Goes without saying I'm also fairly confused at what is going on here. We might have to wait for someone on the team to clarify that behavior.

In the meantime, I hope this helps.

from aspnetcoreodata.

xuzhg avatar xuzhg commented on June 16, 2024

@KnapSac Thanks @julealgon. Changing the codes as below does work as expected:

	public ActionResult< IQueryable > Get(
		ODataQueryOptions< Customer > queryOptions )
	{
		IQueryable< Customer > values = s_Customers.AsQueryable( );

		return Ok(
			queryOptions.ApplyTo(
				values,
				new ODataQuerySettings( ) ) );

        //return Ok(
        //    queryOptions.SelectExpand.ApplyTo(
       //        values,
        //        new ODataQuerySettings()));
    }

image

The reason is that OData serializer needs the SelectExpandClause to decide which properties should be included. In your previous implementation, OData serializer can't get the 'SelectExpandClause' since you call it from queryOptions.SelectExpand.ApplyTo(...).

from aspnetcoreodata.

KnapSac avatar KnapSac commented on June 16, 2024

The reason is that OData serializer needs the SelectExpandClause to decide which properties should be included. In your previous implementation, OData serializer can't get the 'SelectExpandClause' since you call it from queryOptions.SelectExpand.ApplyTo(...).

Ah, that makes sense. And as @julealgon suggested, I can use the ignoreQueryOptions to skip applying e.g. $filter, which I can then apply manually.

Instead of attempting to explicitly apply only the SelectExpand property, you apply the whole ODataQueryOptions object and specify which operations you want to exclude. In this case, we "exclude everything but $select" by passing the negated select option.

However.... when I test this, for whatever reason, I noticed that even if I pass something else on the ignoreQueryOptions, the actual select operation still happens. The only difference I see is that when $select is "blocked", it doesn't show in the @odata.context path.

I can reproduce this behavior too, and to me that does seem like a bug in the library.

from aspnetcoreodata.

julealgon avatar julealgon commented on June 16, 2024

@xuzhg can you take a look at this apparent problem with the partial ApplyTo call? It is incredibly weird to me what is going on, because when I inspect the results after the call, it does seem to respect the ignoreQueryOptions parameter, but when it comes to showing the results, the serializer somehow still applies the selection to it.

When I first saw that, I thought maybe the [EnableQuery] filter was being applied somehow, but I checked the codebase yesterday to be sure and I don't see any existing mechanism that would "automatically" include that filter, so it can't be that.

from aspnetcoreodata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.