Skip to content

Native aot with ~options builder methods~ just the right attributes on the test contexts#648

Merged
gregsdennis merged 31 commits into
native-aotfrom
native-aot-with-options-builder-methods
Feb 3, 2024
Merged

Native aot with ~options builder methods~ just the right attributes on the test contexts#648
gregsdennis merged 31 commits into
native-aotfrom
native-aot-with-options-builder-methods

Conversation

@gregsdennis

@gregsdennis gregsdennis commented Feb 2, 2024

Copy link
Copy Markdown
Collaborator

Update

(see also "original intent" below)

This PR updates all of the serializer contexts back to internal while ensuring that all of the types are supported.

Additionally, it creates several helper methods to work around a known STJ bug. These methods are used throughout the libraries to ensure that the options passed in by the user are respected while still being able to use the serializer context JTIs, which are needed for AOT.

The end result is that contexts are hidden, and users don't need to have annotations for all of the types contained within these libraries; only those which they serialize directly. For example, for JsonSchema, this will likely be only JsonSchema and EvaluationResults.


Original intent of the PR

This implements "sharing" the contexts via JsonSerializerOptions extensions that simply tack on the context to the options' .TypeInfoResolverChain.

There is a single bug that I'm experiencing, and I don't know why. Specifically JsonPointer won't serialize inside the Json.Schema.EvaluationResultsConverter.Write() method. When attempting to serialize the .Details collection (which is IReadOnlyList<EvaluationResults>), I pass in the options, and I have confirmed that at this point all of the resolvers are present in the options. Later, through nested serialization, it comes back into the .Write() method with the JsonSchemaSerializerContext.Default.Options only, which means that it's lost all of the type resolvers somehow, and it fails on JsonPointer.

Now, I know that I can just add JsonPointer to the internal serializer context, but I'd really like to figure out why the options object is changing as this is the root cause.

@github-actions

github-actions Bot commented Feb 2, 2024

Copy link
Copy Markdown

Test Results

    15 files  ±0      15 suites  ±0   14s ⏱️ ±0s
20 478 tests ±0  19 114 ✅ ±0  1 364 💤 ±0  0 ❌ ±0 
20 570 runs  ±0  19 203 ✅ ±0  1 367 💤 ±0  0 ❌ ±0 

Results for commit ddff7d8. ± Comparison against base commit fef56e4.

♻️ This comment has been updated with latest results.

Comment thread JsonSchema/EvaluationResults.cs Outdated
@gregsdennis

gregsdennis commented Feb 2, 2024

Copy link
Copy Markdown
Collaborator Author

I did notice that the serialization, after going into the options.Write for the .Details collection, ends up at the "AOT-aware users should not get this far" line because we don't have an explicit aot-compatible-converter for the collection.

The options still has the full resolver chain at this point. Whatever is happening, it's in the external code between this line and going back into the EvaluationsResultsConverter.

Comment thread JsonPatch/PatchOperation.cs Outdated
Comment thread JsonPath/JsonSerializerOptionsExtensions.cs Outdated
@gregsdennis gregsdennis marked this pull request as ready for review February 2, 2024 03:34
Comment thread JsonLogic/Rules/AllRule.cs
Comment thread JsonLogic/JsonSerializerOptionsExtensions.cs
Comment thread JsonPatch/JsonPatch.cs
Comment thread JsonPatch/JsonPatchSerializerContext.cs Outdated
Comment thread JsonPatch/PatchOperation.cs
Comment thread JsonSchema/JsonSchemaSerializerContext.cs Outdated
/// <param name="typeInfo">An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.</param>
/// <returns>The value that was converted.</returns>
public static void Write<T>(this JsonSerializerOptions options, Utf8JsonWriter writer, T? value, JsonTypeInfo<T>? typeInfo)
public static void Write<T>(this JsonSerializerOptions options, Utf8JsonWriter writer, T value, JsonTypeInfo<T> typeInfo)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I mentioned this in another PR, but the options and typeInfo parameters defined here are redundant and mixing them together can result in consistency problems. Either resolve the typeInfo from the options.GetTypeInfo method or get the Options instance from the typeInfo parameter.

@jevansaks jevansaks Feb 2, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TypeInfo here is used just to get the Converter, and we're using a JsonTypeInfo as parameter to make sure the caller has tagged the type on a JsonSerializerContext. The options parameter is used for all caller-defined types and any other configurations.

@gregsdennis gregsdennis Feb 2, 2024

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm still fuzzy on why the type info is tied to an options in the first place. They seem to be independent concepts.)

Comment thread JsonLogic.Tests/GithubTests.cs Outdated
Comment thread JsonSchema/EvaluationResults.cs Outdated
Comment thread JsonSchema/JsonSchema.cs Outdated

@jevansaks jevansaks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple comments that can be addressed now or later. Please update the PR description based on the work you've done too, it looks a little stale.

Comment thread JsonSchema/EvaluationResults.cs Outdated
@gregsdennis gregsdennis merged commit 587ac2c into native-aot Feb 3, 2024
@gregsdennis gregsdennis deleted the native-aot-with-options-builder-methods branch February 3, 2024 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants