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

Which property type in a DTO to expose Json data #608

Closed
rdelmont opened this issue May 27, 2022 · 4 comments
Closed

Which property type in a DTO to expose Json data #608

rdelmont opened this issue May 27, 2022 · 4 comments
Assignees
Labels

Comments

@rdelmont
Copy link

Hi.

I have some Json data in a column (called userProperties) of my table [Piece] and want to expose that data as a open property in PieceDto.

class PieceDto {
  string name;
  SOME_OPEN_TYPE userProperties;
}

Ideally the response objects would look like:

{
  "name":"Piece 1",
  "userProperties": {
    "property1": "some value",
    "property 2": "another value"
  }
} 

What would be the best type to use for userProperties in the DTO?

Obviously i'd like to be able to filter/select/search in userProperties using requests as follow:
GET Pieces?$filter=userProperties/property1 eq 'some value'
GET Pieces?$search=another

I thought using an open type DictionaryDto based on Dictionary<string, object> or using a JsonDocument but struggle to get filter/select/search to work.
Do you have any thought on that?

@corranrogue9 corranrogue9 self-assigned this May 31, 2022
@corranrogue9
Copy link
Contributor

@rdelmont

I thought using an open type DictionaryDto based on Dictionary<string, object> or using a JsonDocument but struggle to get filter/select/search to work.

Yes, your expectation is correct. We have a known issue here about issues with functions on open types. Your issue might be related to that; to confirm, can you share a bit of the code and queries that you used to get filter, select, etc. to work?

@rdelmont
Copy link
Author

rdelmont commented Jun 14, 2022

@rdelmont

I thought using an open type DictionaryDto based on Dictionary<string, object> or using a JsonDocument but struggle to get filter/select/search to work.

Yes, your expectation is correct. We have a known issue here about issues with functions on open types. Your issue might be related to that; to confirm, can you share a bit of the code and queries that you used to get filter, select, etc. to work?

Currently I use a type DictionaryDto object:

    public class DictionaryDto
    {
        public DictionaryDto()         {            Content = new Dictionary<string, object>();        }

        public IDictionary<string, object> Content { get; set; }
    }

It is used in few objects:

    public class PieceDto
    {
        // Some properties.....

        public DictionaryDto? CustomProperties { get; set; } = null;
    }

I have an automapper configuration that fills this dictionary from a JSON column (named userProperties):

    var pieceMap = CreateMap<Piece, PieceDto>();
    pieceMap.ForMember(dest => dest.CustomProperties, opt => opt.MapFrom(source => JsonSerializer.Deserialize<Dictionary<string, object>>(source.Userproperties)));

To allow filtering I defined a FilterBinder and have overridden the BindDynamicPropertyAccessQueryNode method.
This is kind of working right now as I'm able to do:
GET Pieces?$filter=userProperties/property1 eq 'some value'
but I'm not able to make the $search work.

What's your feeling on this? Is there a better representation to expose this kind of field?

@corranrogue9
Copy link
Contributor

@rdelmont, the issue I mentioned before was about the use of functions, and it doesn't look like you're using a function with your $filter call, so I think it's unrelated. $search should work fine, are you able to share a repro of some kind? Some code with a model, a controller, and an HTTP request?

@corranrogue9
Copy link
Contributor

I think you're doing the right representation with your open type. I'm going to close the issue for now, but if you can provide more details on what is not working about $search, please reopen and we can look at.

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

No branches or pull requests

2 participants