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

[BUG] Azure.AI.DocumentIntelligence: Deserialize ClassifyDocumentOptions, Erroring out on 'Azure.AI.DocumentIntelligence.ClassifierDocumentTypeDetails' #47610

Closed
jaliyaudagedara opened this issue Dec 18, 2024 · 4 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. Cognitive - Form Recognizer customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@jaliyaudagedara
Copy link

jaliyaudagedara commented Dec 18, 2024

Library name and version

Azure.AI.DocumentIntelligence 1.0.0

Describe the bug

Consider the below example:

using Azure.AI.DocumentIntelligence;
using System.Text.Json;

Dictionary<string, ClassifierDocumentTypeDetails> documentTypes = new()
{
    { "Document1", new(new BlobFileListContentSource(new Uri("https://www.microsoft.com"), "fileList1")) },
    { "Document2", new(new BlobFileListContentSource(new Uri("https://www.microsoft.com"), "fileList2")) }
};

BuildClassifierOptions buildClassifierOptions = new("someClassifierId", documentTypes);

string json = JsonSerializer.Serialize(buildClassifierOptions, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);
// {
//   "ClassifierId": "someClassifierId",
//   "Description": null,
//   "BaseClassifierId": null,
//   "DocumentTypes": {
//     "Document1": {
//       "BlobSource": null,
//       "BlobFileListSource": {
//         "ContainerUri": "https://www.microsoft.com",
//         "FileList": "fileList1"
//       },
//       "SourceKind": null
//     },
//     "Document2": {
//       "BlobSource": null,
//       "BlobFileListSource": {
//         "ContainerUri": "https://www.microsoft.com",
//         "FileList": "fileList2"
//       },
//       "SourceKind": null
//     }
//   },
//   "AllowOverwrite": null
// }

buildClassifierOptions = JsonSerializer.Deserialize<BuildClassifierOptions>(json, new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true
});

This is erroring out on Deserialize;

Unhandled exception. System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Azure.AI.DocumentIntelligence.ClassifierDocumentTypeDetails'. Path: $.DocumentTypes.Document1 | LineNumber: 5 | BytePositionInLine: 18.
 ---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Azure.AI.DocumentIntelligence.ClassifierDocumentTypeDetails'.
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, Exception innerException)
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(JsonTypeInfo typeInfo, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonDictionaryConverter`3.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TDictionary& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5.TryRead[TArg](ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo, TArg& arg)
   at System.Text.Json.Serialization.Converters.SmallObjectWithParameterizedConstructorConverter`5.ReadAndCacheConstructorArgument(ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo)
   at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.<Main>$(String[] args) in C:\Users\Jaliya\Desktop\ConsoleApp2\ConsoleApp2\Program.cs:line 16

Expected behavior

string is correctly deserialized to given type.

Actual behavior

Deserialization is erroring out.

Reproduction Steps

Given above

Environment

dotnet: 9.0

@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Dec 18, 2024
@jaliyaudagedara
Copy link
Author

@kinelski, saw this:

public partial class ClassifierDocumentTypeDetails : IUtf8JsonSerializable, IJsonModel<ClassifierDocumentTypeDetails>

There we have DeserializeClassifierDocumentTypeDetails.

@kinelski kinelski self-assigned this Dec 18, 2024
@kinelski kinelski removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Dec 18, 2024
@github-actions github-actions bot added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Dec 18, 2024
@kinelski kinelski added Cognitive - Form Recognizer Client This issue points to a problem in the data-plane of the library. labels Dec 18, 2024
@kinelski
Copy link
Member

@jaliyaudagedara,

When serializing/deserializing .NET SDK models, you must use our JsonModelConverter (explained here):

JsonSerializerOptions options = new JsonSerializerOptions()
{
    Converters = { new JsonModelConverter() }
};

InputModel model = new InputModel();
string data = JsonSerializer.Serialize(model, options);

This will fix the issue you're seeing with ClassifierDocumentTypeDetails.

That said, please be aware that models AnalyzeBatchDocumentsOptions, AnalyzeDocumentOptions, and ClassifyDocumentOptions currently have a bug that prevents some of their properties from being serialized (#47612). It shouldn't affect you if you're only working with BuildClassifierOptions.

@kinelski kinelski added issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close. and removed needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team labels Dec 18, 2024
Copy link

Hi @jaliyaudagedara. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

@jaliyaudagedara
Copy link
Author

jaliyaudagedara commented Dec 19, 2024

@kinelski,

Thank you very much for your prompt response. I am sorry, I couldn't find the documentation. Thanks for sharing.

With JsonModelConverter, Confirming serialization/deserialization works for BuildClassifierOptions. And it also works for BuildDocumentModelOptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. Cognitive - Form Recognizer customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

2 participants