corvus-dotnet / Corvus.JsonSchema

Support for Json Schema validation and entity generation
Apache License 2.0
106 stars 8 forks source link

Virtual static method 'FromProperties' is not implemented on type #337

Closed etcvee closed 2 months ago

etcvee commented 6 months ago

Hi,

There is an issue when trying to use generated by the generatejsonschematypes code from .NET Standard 2.0 class library in .NET 8.0 project:

System.TypeLoadException: "Virtual static method 'FromProperties' is not implemented on type 'GeneratedClasses.JsonSchema' from assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."

The exception dissapears when switching the class library to net8.0 in csproj.

Tool version is:

generatejsonschematypes -v
3.0.2+f43f8e99c7214c06fb3fd398aa8120f550b85f6a

Repro steps:

  1. Create a json schema

    {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Test schema",
    "type": "object",
    "properties": {
        "id": {
            "type": "integer"
        }
    }
    }
  2. Generate C# classes from the json schema (I used GeneratedClasses for namespace)

  3. Put generated classes into class library and targets netstandard 2.0

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <LangVersion>12.0</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <Using Include="System" />
        <Using Include="System.IO" />
        <Using Include="System.Collections.Generic" />
        <Using Include="System.Linq" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Corvus.Json.ExtendedTypes" Version="3.0.2" />
    </ItemGroup>

</Project>
  1. Create a console app, reference the class library and use the generated class:
using Corvus.Json;
using GeneratedClasses;

var parsed = JsonSchema.Parse("{}");
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>corvus_tests</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
    </ItemGroup>

</Project>
  1. Run the app - System.TypeLoadException will be thrown
mwadams commented 6 months ago

Can you test this project out on your machine?

I have generated the code with v3.0.2. I do not see this issue.

JsonSchemaSample.zip

mwadams commented 6 months ago

I can reproduce this issue. It appears to be trying to load the net8.0 version of Corvus.Json.ExtendedTypes despite the transient dependency on the netstandard2.0 version.

mwadams commented 6 months ago

The exception would also disappear if your exe was targetting a vesion of dotnet prior to net8.0

One way to fix this would be to multitarget your netstandard2.0 library.

Replace

<TargetFramework>netstandard2.0</TargetFramework>

with

<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
etcvee commented 6 months ago

I've added <TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks> and it works correctly. Also I've created a netcoreapp3.1 project and with the same target frameworks it also works correctly.

Many thanks!

mwadams commented 6 months ago

That's fantastic. I will add documentation to the README to cover off this case and to address #340 at the same time.

mwadams commented 2 months ago

Fixed in #393 - please try the Preview packages on nuget, and examine the updated README.md