eiriktsarpalis / stj-schema-mapper

A component mapping System.Text.Json contracts to JSON schema documents
MIT License
5 stars 1 forks source link

[JsonSchemaMapper v8] Incorrect type schema produced in Native-AOT scenarios #7

Closed SergeyMenshykh closed 3 days ago

SergeyMenshykh commented 4 days ago

Description

JsonSchemaMapper v8 produces an incorrect JSON Schema in a Native-AOT scenarios. However, in non-Native-AOT scenarios, it works as expected and generates the correct schema.

Type for which to generate the schema

internal sealed class Location
{
    public string City { get; set; }
    public string Country { get; set; }

    public Location(string city, string country)
    {
        this.City = city;
        this.Country = country;
    }
}

Expected result

{
  "type": "object",
  "properties": {
    "City": { "type": "string" },
    "Country": { "type": "string" }
  },
  "required": [ "City", "Country" ]
}

Actual result

{
  "type": "object",
  "properties": {
    "City": { "type": [ "string", "null" ] },
    "Country": { "type": [ "string", "null" ] }
  }
}

Steps to reproduce

  1. Publish the TypeSchemaGenerationInAOT.zip console application using the dotnet publish -r win-x64 command,
  2. Run it .\bin\Release\net8.0\win-x64\publish\TypeSchemaGenerationInAOT.exe and observe the results.

Envornment

.net 8, JsonSchemaMapper v8 source code

eiriktsarpalis commented 4 days ago

This is due to the implementation targeting System.Text.Json v8 using reflection to resolve certain metadata such as nullability annotations and constructor parameters. It can be worked around by disabling the IlcTrimMetadata property in the application's project configuration.

The implementation targeting System.Text.Json v9 is not impacted by this issue since it sources all its metadata from the STJ source generator.