Merge branch 'v9/cyclehack/autogeneration_of_json_schema' of https://github.com/umbraco/Umbraco-CMS into v9/cyclehack/autogeneration_of_json_schema
This commit is contained in:
@@ -22,38 +22,28 @@ namespace JsonSchema
|
||||
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
Parser.Default.ParseArguments<Options>(args)
|
||||
.WithParsedAsync<Options>(Execute);
|
||||
try
|
||||
{
|
||||
await Parser.Default.ParseArguments<Options>(args)
|
||||
.WithParsedAsync<Options>(Execute);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static async Task Execute(Options options)
|
||||
{
|
||||
var result = GenerateJsonSchema();
|
||||
var generator = new UmbracoJsonSchemaGenerator();
|
||||
var schema = await generator.Generate();
|
||||
|
||||
var path = Path.Combine(Environment.CurrentDirectory, options.OutputFile);
|
||||
await File.WriteAllTextAsync(path, result);
|
||||
|
||||
await File.WriteAllTextAsync(path, schema);
|
||||
|
||||
Console.WriteLine("File written at " + path);
|
||||
}
|
||||
|
||||
private static string GenerateJsonSchema()
|
||||
{
|
||||
var settings = new JsonSchemaGeneratorSettings()
|
||||
{
|
||||
SchemaType = SchemaType.JsonSchema,
|
||||
AlwaysAllowAdditionalObjectProperties = true,
|
||||
SerializerSettings = new JsonSerializerSettings(),
|
||||
TypeNameGenerator = new UmbracoPrefixedTypeNameGenerator(),
|
||||
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull
|
||||
};
|
||||
settings.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
|
||||
var generator = new JsonSchemaGenerator(settings);
|
||||
|
||||
var schema = generator.Generate(typeof(AppSettings));
|
||||
|
||||
return schema.ToJson(Formatting.Indented);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
49
src/JsonSchema/UmbracoJsonSchemaGenerator.cs
Normal file
49
src/JsonSchema/UmbracoJsonSchemaGenerator.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NJsonSchema.Generation;
|
||||
|
||||
namespace JsonSchema
|
||||
{
|
||||
public class UmbracoJsonSchemaGenerator
|
||||
{
|
||||
private readonly JsonSchemaGenerator _innerGenerator;
|
||||
private static readonly HttpClient s_client = new HttpClient();
|
||||
|
||||
public UmbracoJsonSchemaGenerator()
|
||||
{
|
||||
_innerGenerator = new JsonSchemaGenerator(new UmbracoJsonSchemaGeneratorSettings());
|
||||
}
|
||||
|
||||
public async Task<string> Generate()
|
||||
{
|
||||
var umbracoSchema = GenerateUmbracoSchema();
|
||||
var officialSchema = await GetOfficialAppSettingsSchema();
|
||||
|
||||
officialSchema.Merge(umbracoSchema);
|
||||
|
||||
return officialSchema.ToString();
|
||||
}
|
||||
|
||||
private async Task<JObject> GetOfficialAppSettingsSchema()
|
||||
{
|
||||
|
||||
var response = await s_client.GetAsync("https://json.schemastore.org/appsettings.json");
|
||||
|
||||
|
||||
var result = await response.Content.ReadAsStringAsync();
|
||||
|
||||
return JsonConvert.DeserializeObject<JObject>(result);
|
||||
|
||||
}
|
||||
|
||||
private JObject GenerateUmbracoSchema()
|
||||
{
|
||||
var schema = _innerGenerator.Generate(typeof(AppSettings));
|
||||
|
||||
return JsonConvert.DeserializeObject<JObject>(schema.ToJson());
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/JsonSchema/UmbracoJsonSchemaGeneratorSettings.cs
Normal file
22
src/JsonSchema/UmbracoJsonSchemaGeneratorSettings.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using NJsonSchema.Generation;
|
||||
using NJsonSchema.Generation.TypeMappers;
|
||||
using Umbraco.Cms.Core;
|
||||
|
||||
namespace JsonSchema
|
||||
{
|
||||
public class UmbracoJsonSchemaGeneratorSettings : JsonSchemaGeneratorSettings
|
||||
{
|
||||
public UmbracoJsonSchemaGeneratorSettings()
|
||||
{
|
||||
AlwaysAllowAdditionalObjectProperties = true;
|
||||
SerializerSettings = new JsonSerializerSettings();
|
||||
TypeNameGenerator = new UmbracoPrefixedTypeNameGenerator();
|
||||
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull;
|
||||
|
||||
SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user