RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.37k stars 529 forks source link

Cannot Derive from sealed type 'int' when generating CSharp code from OpenAPI V2.0 Schema #698

Open KeyboardNerd opened 6 years ago

KeyboardNerd commented 6 years ago

When using CSharpGenerator for OpenAPI v2.0 Json Schema, the generated code contains the following error:

'MinLength': cannot derive from sealed type 'int' ConsoleApplication1

I think this is related to 'MinLength' is using $ref to reference the type on this line. This is reproducible on NJsonChema.CodeGeneration version 9.10.46

Do you have any work around?

Thanks.

The code I'm using is as following:

NJsonSchema.JsonSchema4 schema = await NJsonSchema.JsonSchema4.FromFileAsync(@"C:\Dev\schema.json");
var settings = new NJsonSchema.CodeGeneration.CSharp.CSharpGeneratorSettings {  Namespace = "ConsoleApplication1" };
var generator = new NJsonSchema.CodeGeneration.CSharp.CSharpGenerator(schema, settings);
var fileString = generator.GenerateFile();
System.IO.File.WriteAllText(@"C:\Dev\schema.cs", fileString);
RicoSuter commented 6 years ago

Can you post a part of the generated code?

KeyboardNerd commented 6 years ago

This is where build fails:

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.10.46.0 (Newtonsoft.Json v9.0.0.0)")]
    public partial class MinLength : int, System.ComponentModel.INotifyPropertyChanged
    {

        public string ToJson() 
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public static MinLength FromJson(string data)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<MinLength>(data);
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) 
                handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
RicoSuter commented 6 years ago

allOf is handled as inheritance:

    "positiveIntegerDefault0": {
        "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
    },

This is a bug or enhancement. Inheritance should not be applied when schema is any or a primitive type.

RicoSuter commented 6 years ago

Can you set `"type": "integer"?

RicoSuter commented 6 years ago

BTW: I recommend to just use NSwag.Core for a complete Swagger 2.0 (and 3.0) model...

Palsskv commented 2 weeks ago

FYI, when generating CSharp types for the OData 4 CSDL schema, I'm getting the same error: edm.cs(550, 52): [CS0509] 'PositiveIntegerDefault0': cannot derive from sealed type 'int'

Generated code:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "11.0.2.0 (Newtonsoft.Json v13.0.0.0)")]
public partial class PositiveIntegerDefault0 : int
{

}

positiveInteger is a type defined in https://json-schema.org/draft-04/schema, so this might come up for other schemas that reference it.

This isn't critical for my work, just sharing info.