-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Source not being mapped in Json return from minimal API #8282
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
Comments
Hi @werto165, Manually (de-)serializing any of the Elasticsearch client types is generally unsupported. You might be lucky when using One reason for this is, that internal converters are not registered for the default Another reason is, that types do not neccessarily support round-tripping. E.g. request-only types can only be serialized while response-only types can only be deserialized. This can not be worked around. If the response uses one of these types, you are out of luck. We usually suggest to create a custom POCO class / model to which you assign all the required properties from the Elasticsearch client response. This could for example look like this: var response = new ResponseModel<Individual>
{
Took = clientResponse.Took,
// ...
Hits = new HitsModel<Individual>[]
{
new HitsModel<Individual>
{
Source = clientResponse.Source
// ...
}
}
} Btw.: Re-serializing the actual document (type: Please let me know if I answered your question. |
Hi there, Thanks for the response. I am quite new to elastic search in general, so apologies for the trivial question again, but it seems strange that I can't have a wrapper around the normal REST API when that is the response you get from the actual API i.e SearchResponse, I have cross checked most of the fields on this response type and it seems to marry up with what is being returned by the actual REST API. I will give the custom POCO a go. With regards to your comment about (de-)serialization are you saying that the _source returned from the response may also be used elsewhere in requests? Therefore this field is not serialized properly? If you could answer another query I have it would be greatly appreciated, I have types that define my mappings, I am using the old client to create my mapping for the index. With this mapping I have set dynamic to "strict" which has allowed me to enforce that when passing an update for example: POST myindex/_update it throws an error. However, I am having trouble enforcing types, so if in my mapping I set the type to "text" for example, in the update i can set this field to whatever data type such as integer, this creates a problem in C# because the class is expecting a string but with the update it has allowed me to change it to an int. Therefore when deserializing the response into the object it throws an error. Are there any mechanisms that can prevent changing types with the API? |
Hi @werto165,
This is absolutely no problem. Returning, using, wrapping the
I'm not quite sure I can completely follow that question, but it seems like server side behavior. It would be the best to ask this in our Community Forums. I'm going to close this issue for now, but feel free to open another one if you have problems related to the .NET client. |
Uh oh!
There was an error while loading. Please reload this page.
Elastic.Clients.Elasticsearch version:
8.14.6
Elasticsearch version:
8.14.0
.NET runtime version:
.NET8
Operating system version:
Windows 11
Description of the problem including expected versus actual behavior:
I first noticed this issue when trying to return SearchResponse from a minimal api endpoint. There is something going wrong with the serialization at some point in the serialization chain.
When using minimal APIs there is a silent error (no exceptions or anything untoward) when using System.Text.Json.JsonSerializer the default serializer used for response bodies, this results in the message "can't parse JSON" (shows up in swagger only, so potentially malformed json?) when running a search returning the type Results<Ok<SearchResponse>>.
JsonResponse
Output
return from endpoint
Steps to reproduce:
Expected behavior
The source value should be mapped but isn't, may be erroring because there is a missing comma from the return type, that is why the json is malformed. As there should be a value which is created but isn't be created?
JsonResponse returns source just fine.
output doesn't return the source value. This is the same or similar to what is being used for serialization by ASPNET.
I have a feeling it is related to the following code:
in IntermediateSourceConverter.cs
When it works it hits only the Read code, when it doesn't it doesn't hit the Write because the type check always fails.
{Elastic.Clients.Elasticsearch.Serialization.SourceConverter} (READ function)
{System.Text.Json.Serialization.Converters.ObjectDefaultConverter<Elastic.Clients.Elasticsearch.Serialization.SourceMarker>} (WRITE function)
The text was updated successfully, but these errors were encountered: