NJsonSchema
NJsonSchema copied to clipboard
Unable to update an old script with NJsonSchema4 to a newer version
Hello, Could you please help me with updating an old script (2018) with the new classes available now, because the old script can't be run anymore in LinqPad.
I want to run the the existing script on a yaml file with dotnet 3.1.
It complains about the SwaggerDocument, SwaggerToCSharpClientGeneratorSettings, SwaggerToCSharpClientGenerator and JsonProperty: The type or namespace name 'JsonProperty' could not be found.
I have used the following imports, but no success:
<NuGetReference>NSwag.CodeGeneration.CSharp</NuGetReference>
<NuGetReference>YamlDotNet</NuGetReference>
<NuGetReference>NSwag.Core</NuGetReference>
<NuGetReference>NSwag.CodeGeneration</NuGetReference>
<NuGetReference>NJsonSchema</NuGetReference>
<NuGetReference>System.Globalization</NuGetReference>
<NuGetReference>System.Text.RegularExpressions</NuGetReference>
<NuGetReference>NJsonSchema.CodeGeneration</NuGetReference>
<NuGetReference>NJsonSchema.CodeGeneration.CSharp</NuGetReference>
<Namespace>NSwag</Namespace>
<Namespace>NSwag.CodeGeneration</Namespace>
<Namespace>NSwag.CodeGeneration.CSharp</Namespace>
<Namespace>System.Globalization</Namespace>
<Namespace>YamlDotNet.Serialization</Namespace>
<Namespace>NJsonSchema</Namespace>
<Namespace>System.Text.RegularExpressions</Namespace>
<Namespace>NJsonSchema.CodeGeneration</Namespace>
<Namespace>NJsonSchema.CodeGeneration.CSharp</Namespace>
object yamlObject;
using(var reader = new StreamReader(Path.GetDirectoryName(Util.CurrentQueryPath) + @"\zuora-swagger-stripped.yaml"))
{
var deserializer = new DeserializerBuilder().Build();
yamlObject = deserializer.Deserialize(reader);
}
var serializer = new SerializerBuilder()
.JsonCompatible()
.Build();
var json = serializer.Serialize(yamlObject);
var document = await SwaggerDocument.FromJsonAsync(json);
var settings = new SwaggerToCSharpClientGeneratorSettings
{
ClassName = "ZuoraClient",
CSharpGeneratorSettings =
{
Namespace = "SalesGateway.ZuoraClient.Dto",
ClassStyle = NJsonSchema.CodeGeneration.CSharp.CSharpClassStyle.Poco,
},
GenerateExceptionClasses = false,
};
settings.CSharpGeneratorSettings.GenerateJsonMethods = false;
settings.CSharpGeneratorSettings.ArrayType = "System.Collections.Generic.List";
settings.CSharpGeneratorSettings.TypeNameGenerator = new CustomTypeNameGenerator();
settings.CSharpGeneratorSettings.PropertyNameGenerator = new CustomPropertyNameGenerator();
settings.CSharpGeneratorSettings.EnumNameGenerator = new CustomEnumNameGenerator();
settings.CSharpGeneratorSettings.GenerateDataAnnotations = true;
settings.CSharpGeneratorSettings.DictionaryBaseType = "DictionaryBaseTypeNotSupported";
var generator = new SwaggerToCSharpClientGenerator(document, settings);
var code = generator.GenerateFile(ClientGeneratorOutputType.Contracts);
code = Regex.Replace(code, Regex.Escape(settings.CSharpGeneratorSettings.Namespace) + @"\.bool\.(\w+)", match => match.Groups[1].Value.ToLower());
code = Regex.Replace(code, @"DictionaryBaseTypeNotSupported\<[^>]+\>", "object");
using(var writer = new StreamWriter(Path.GetDirectoryName(Util.CurrentQueryPath) + @"\ZuoraDto.cs", false))
{
writer.Write(code);
}
class CustomEnumNameGenerator : IEnumNameGenerator
{
Regex re = new Regex(@"[()]", RegexOptions.Compiled);
public string Generate(int index, string name, object value, JsonSchema schema)
{
return re.Replace(ConversionUtilities.ConvertToUpperCamelCase(name.Replace(":", "-"), true).Replace(".", "_").Replace("#", "_"), "");
}
}
class CustomPropertyNameGenerator : CSharpPropertyNameGenerator
{
Regex re = new Regex(@"[()]", RegexOptions.Compiled);
public override string Generate(JsonProperty property)
{
return re.Replace(base.Generate(property), "");
}
}
class CustomTypeNameGenerator : DefaultTypeNameGenerator
{
public override string Generate(JsonSchema schema, string typeNameHint, IEnumerable<string> reservedTypeNames)
{
var name = base.Generate(schema, typeNameHint, reservedTypeNames).Replace("&", "");
if (name == "Type")
{
return "RequestType";
}
return name;
}
}
Thank you for your help!