Add test coverage for System.Text.Json source generation compatibility #854
+164
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Validates that Fluid's
jsonfilter works correctly when users supply source-generatedJsonSerializerContextmetadata viaTemplateOptions.JsonSerializerOptions.Changes
Fluid.Tests/JsonSourceGenTests.csFluidJsonContextwith camelCase naming policySourceGenPersonandSourceGenAddressfor test scenariosUsage Pattern
Key Implementation Details
DefaultJsonTypeInfoResolverto support both user types and Fluid's internalFluidValuetypesJsonSerializerOptionsitself, not just in source-gen context optionsJsonSerializer.Serializecalls with same contextOriginal prompt
Summary
Add a new test suite to validate that Fluid's
jsonfilter andFluidValueJsonConverterwork correctly when callers supplySystem.Text.Jsonsource‑generated metadata via aJsonSerializerContext. Users reported thatTemplateOptions.JsonSerializerOptions(and transitivelyTemplateContext.JsonSerializerOptions) did not support source generation scenarios. These tests demonstrate compatibility and help prevent regressions.Rationale
Fluid integrates with
System.Text.JsonthroughTemplateOptions.JsonSerializerOptionsand a custom[JsonConverter]onFluidValue. .NET source generation usesJsonSerializerContextinstances added toJsonSerializerOptions.TypeInfoResolverChain(or olderAddmethods). If the converter or thejsonfilter sidesteps or mutates options incorrectly, serialization might fall back to reflection or fail to locate generated metadata. The tests:JsonSerializer.Serializecalls using the same context.FluidValueinstances.Added File
Create a new test file
Fluid.Tests/JsonSourceGenTests.cscontaining:FluidJsonContextwith[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]and several[JsonSerializable]type declarations.PersonandAddressused for nested serialization tests.JsonFilter_UsesSourceGeneratedContext_ForSimpleObject– end‑to‑end object serialization.JsonFilter_UsesSourceGen_ForArraysAndDictionaries– arrays and dictionaries.JsonFilter_FluidValueWrapping_DoesNotBreakSourceGen– directFluidValueinstances and mixed arrays.JsonFilter_RespectsCamelCaseNamingPolicy_FromSourceGen– asserts naming policy.Test Implementation Notes
Fluid.Tests.csproj.System.Text.Jsonfor these TFMs.TemplateOptions.MemberAccessStrategy.Register<T>()for model types andWithMiscFilters()extension to ensure thejsonfilter is available.Template.TryParsesucceeds and reports errors if parsing fails.Non-Goals
UnsafeRelaxedJsonEscaping).Future Enhancements (Optional)
Additional tests could be added later for:
JavaScriptEncoderbehavior with source generation.File Content