Search SDK: FieldBuilder tries to read a property marked with JsonIgnore
See original GitHub issueWe are using Microsoft.Azure.Search.dll (3.0.0) from NuGet Package 3.0.3
Here are our Interfaces:
public interface IResourceSearchDocument
{
string RecordType { get; set; }
string Title { get; set; }
string Description { get; set; }
DateTime? PublishedDate { get; set; }
DateTime? LastUpdatedDate { get; set; }
string ImageUrl { get; set; }
string LinkToResource { get; set; }
}
public interface IEventSearchDocument
{
string EventId { get; set; }
string ProductId { get; set; }
string EventCode { get; set; }
string EventType { get; set; }
string Location { get; set; }
DateTime? EventStartDate { get; set; }
DateTime? EventEndDate { get; set; }
}
This is our base class:
[SerializePropertyNamesAsCamelCase]
public abstract class SearchDocument : ISearchDocument
{
/// <summary>
/// Gets or sets the ID for the document. For consistency, this should never be updated or retrieved manually.
/// All IDs should be set through the <see cref="Id"/> property.
/// </summary>
[Key]
[JsonProperty("Id")]
public string AzureId { get; set; }
/// <summary>
/// Gets or sets any unique IDs or compound IDs that might contain characters unsafe for transmission via URL.
/// For consistency, all IDs should be set through this property.
/// </summary>
[JsonIgnore]
[IsRetrievable(false)]
public string Id
{
get
{
return AzureId.FromBase64EncodedString();
}
set
{
AzureId = value.ToBase64EncodedString();
}
}
}
Finally, this is the WebSiteSearchDocument:
[SerializePropertyNamesAsCamelCase]
public class WebSearchDocument : SearchDocument, IEventSearchDocument, IResourceSearchDocument
{
#region Implementation of IEventSearchDocument
[IsRetrievable(true)]
public string EventId { get; set; }
[IsRetrievable(true)]
public string ProductId { get; set; }
[IsRetrievable(true)]
public string EventCode { get; set; }
[IsRetrievable(true), IsFilterable]
public string EventType { get; set; }
public string Location { get; set; }
[IsRetrievable(true), IsFilterable]
public DateTime? EventStartDate { get; set; }
[IsRetrievable(true), IsFilterable]
public DateTime? EventEndDate { get; set; }
#endregion Implementation of IEventSearchDocument
#region Implementation of IResourceSearchDocument
[IsRetrievable(true), IsFilterable]
public string RecordType { get; set; }
[JsonIgnore]
[IsRetrievable(false)]
public WebSearchRecordType RecordTypeEnum { get; set; }
[IsRetrievable(true), IsSearchable]
public string Title { get; set; }
[IsRetrievable(true), IsSearchable]
public string Description { get; set; }
[IsRetrievable(true)]
public DateTime? PublishedDate { get; set; }
[IsRetrievable(true)]
public DateTime? LastUpdatedDate { get; set; }
[IsRetrievable(true)]
public string ImageUrl { get; set; }
[IsRetrievable(true)]
public string LinkToResource { get; set; }
#endregion Implementation of IResourceSearchDocument
}
We have set up an IndexManager and this method is called to ‘Create the Index’:
public Index Create<T>(string indexName) where T : class, ISearchDocument
{
if (Exists(indexName))
{
throw new ArgumentException($"An index by the name of {indexName} already exists.", nameof(indexName));
}
var indexDefinition = new Index
{
Name = indexName,
Fields = FieldBuilder.BuildForType<T>()
};
return _serviceClient.Indexes.Create(indexDefinition);
}
This is the exception we are getting back when we try to set the enum to JsonIgnore and IsRetrievable(false)
{“Property recordTypeEnum has unsupported type BACP.Web.Search.WebSearchRecordType\r\nParameter name: propertyType”}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147024809
HelpLink: null
IPForWatsonBuckets: {140706498970467}
InnerException: null
IsTransient: false
Message: “Property recordTypeEnum has unsupported type BACP.Web.Search.WebSearchRecordType\r\nParameter name: propertyType”
ParamName: “propertyType”
RemoteStackTrace: null
Source: “Microsoft.Azure.Search”
StackTrace: " at Microsoft.Azure.Search.FieldBuilder.GetDataType(Type propertyType, String propertyName)\r\n at Microsoft.Azure.Search.FieldBuilder.BuildForType[T](IContractResolver contractResolver)\r\n at Felinesoft.Search.IndexManager.<CreateAsync>d__51.MoveNext() in E:\\Felinesoft\\BACP\\DEV\\WEB\\BACP.Web\\Felinesoft.Search\\IndexManager.cs:line 86\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\r\n at Felinesoft.Search.IndexManager.<RebuildAsync>d__7`1.MoveNext() in E:\Felinesoft\BACP\DEV\WEB\BACP.Web\Felinesoft.Search\IndexManager.cs:line 117\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtim
e.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at BACP.Web.Presentation.Infrastructure.Azure.SearchIndexBuilder.<>c__DisplayClass26_0.<<GetAsyncIndexBuilders>b__4>d.MoveNext() in E:\Felinesoft\BACP\DEV\WEB\BACP.Web\BACP.Web.Presentation\Infrastructure\Azure\SearchIndexBuilder.cs:line 180\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at BACP.Web.Presentation.Controllers.AzureSearchToolsController.<RebuildIndex>d__6.MoveNext() in E:\Felinesoft\BACP\DEV\WEB\BACP.Web\BACP.Web.Presentation\Controllers\AzureSearchToolsController.cs:line 52"
TargetSite: {Microsoft.Azure.Search.Models.DataType GetDataType(System.Type, System.String)}
WatsonBuckets: null
_HResult: -2147024809
_className: null
_data: {System.Collections.ListDictionaryInternal}
_dynamicMethods: null
_exceptionMethod: {Microsoft.Azure.Search.Models.DataType GetDataType(System.Type, System.String)}
_exceptionMethodString: null
_helpURL: null
_innerException: null
_ipForWatsonBuckets: {140706498970467}
_message: “Property recordTypeEnum has unsupported type BACP.Web.Search.WebSearchRecordType”
_remoteStackIndex: 0
_remoteStackTraceString: null
_safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
_source: “Microsoft.Azure.Search”
_stackTrace: {sbyte[768]}
_stackTraceString: null
_watsonBuckets: null
_xcode: -532462766
_xptrs: {0}
m_paramName: “propertyType”
It is my understanding that putting the JsonIgnore and IsRetrievable(false) attributes on the RecordTypeEnum property, then the Microsoft.Azure.Search.Models.DataType GetDataType(System.Type, System.String)
should ignore it.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
The fix for this issue shipped with version 3.0.5 to NuGet.
To make your workaround slightly more generic you could change
to
That way all properties marked with the JsonIgnore attribute will be ignored when building your fields.