RicoSuter / NJsonSchema

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

Code Generation is not working #680

Open dula85 opened 6 years ago

dula85 commented 6 years ago

I am trying out the NJsonSchema as bellow to generate the POCO classes with http://petstore.swagger.io/v2/swagger.json

My code is as bellow,

       string path = @"c:\temp\generated_code_json.txt";
        var task = JsonSchema4.FromUrlAsync("http://petstore.swagger.io/v2/swagger.json");
        task.Wait();
        var doc = task.Result;

        var schema = JsonSchema4.FromJsonAsync(doc.ToJson());
        var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace "ns" };

        var generator = new CSharpGenerator(schema.Result, settings);
        var code = generator.GenerateFile();
        File.WriteAllText(path, code);
        Console.Write(code);
        Console.ReadKey();

but output is as bellow,

//---------------------- // // Generated using the NJsonSchema v9.10.45.0 (Newtonsoft.Json v9.0.0.0) (http://NJsonSchema.org) // //----------------------

namespace ns {

pragma warning disable // Disable all warnings

}

I am wondering what is the reason for not generating POCO classes

RicoSuter commented 6 years ago

The url does not serve a JSON Schema but a Swagger specification, use http://nswag.org to load and generate code with it...

dula85 commented 6 years ago

thank you for the reply and I tried with nswag as bellow,

       string path = @"c:\temp\generated_code_json.txt";
        var task = SwaggerYamlDocument.FromUrlAsync("http://petstore.swagger.io/v2/swagger.json");
        task.Wait();
        var document = task.Result;
        var schema = JsonSchema4.FromJsonAsync(document.ToJson());

        var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns" };

        var generator = new CSharpGenerator(schema.Result, settings);
        var code = generator.GenerateFile();
        File.WriteAllText(path, code);
        Console.Write(code);
        Console.ReadKey();

but still I am getting same output. am I doing something incorrectly ? what I really want is that generate the classes using swagger url and Csharp DTO templates. Is it possible to achieve ?

RicoSuter commented 6 years ago

Try SwaggerDocument.FromUrlAsync and use SwaggerToCSharpClientGenerator with the document

dula85 commented 6 years ago

I used SwaggerYamlDocument and SwaggerToCSharpControllerGenerator and get code generated successfuly, but I don't want client code but only need POCO classes and need to add some custom code. Would you recommend some approaches ?

RicoSuter commented 6 years ago

There’s a setting to disable client generation. GenerateClients?

dula85 commented 6 years ago

Is that setting is ClientGeneratorOutputType ?