1

I have a series of JSON schema files that I'm trying to output to POCOs, and have the NJsonSchema.CodeGeneration.CSharp library loaded for this purpose. In specifying a namespace for the models, it does not appear that the namespace is actually being output into the generated code files. The POCOs are being written, but no namespace declaration surrounds the objects. I am expecting a namespace MyApp.DataModels to be in each file. What am I doing wrong?

Here are the relevant pieces of code:

CSharpGeneratorSettings csGenSettings = new CSharpGeneratorSettings() {
    ClassStyle = CSharpClassStyle.Poco,
    HandleReferences = true,
    Namespace = "MyApp.DataModels"
};

var _resolver = new CSharpTypeResolver(csGenSettings);
_resolver.RegisterSchemaDefinitions(swaggerDoc.Definitions);
var generator = new CSharpGenerator(swaggerDoc, csGenSettings, _resolver);
var typeDef = generator.GenerateTypes();

foreach (CodeArtifact codeArtifact in typeDef.Artifacts)
{
    File.WriteAllText(Path.Combine(dirDestination, codeArtifact.TypeName + ".cs"), codeArtifact.Code);
}
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
theMayer
  • 15,456
  • 7
  • 58
  • 90
  • I'm on an older version of NJsonSchema where `GenerateTypes()` is not available, but I assume it only generates the classes. When calling `.GenerateFile()` it generates all classes inside a single `namespace { ... }` block. – user247702 Nov 07 '18 at 15:52
  • That being said, your use case might just be an oversight by the author. Try raising an issue at https://github.com/RSuter/NJsonSchema or https://gitter.im/NJsonSchema/NJsonSchema, the author is fairly responsive. – user247702 Nov 07 '18 at 15:54
  • Sure, it might be a bug. Hopefully he sees this tag and follows issues here - if it is a bug, that might be the answer, or it might not be a bug, in which case I'm just using it wrong... – theMayer Nov 07 '18 at 16:19

1 Answers1

0

Call this method:

https://github.com/RSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs#L83

With your collection to render the header/footer, ie namespace...

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
  • I will give it a try and see what happens. For now, I've been just manually surrounding it with the namespace info. Good work on this project overall, it is very useful! – theMayer Nov 10 '18 at 22:31