-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ship two json schema files.. One that reference others, and a CMS one (…
…#13123) * Ship two json schema files.. One that reference others, and a CMS one * git ignore * Build schema files sepearately
- Loading branch information
Showing
10 changed files
with
245 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,83 @@ | ||
// Copyright (c) Umbraco. | ||
// See LICENSE for more details. | ||
|
||
using Microsoft.Extensions.FileProviders; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using NJsonSchema.Generation; | ||
using Umbraco.Cms.Core.Configuration.Models; | ||
|
||
namespace JsonSchema | ||
namespace JsonSchema; | ||
|
||
/// <summary> | ||
/// Generator of the JsonSchema for AppSettings.json including A specific Umbraco version. | ||
/// </summary> | ||
public class UmbracoJsonSchemaGenerator | ||
{ | ||
private static readonly HttpClient s_client = new(); | ||
private readonly JsonSchemaGenerator _innerGenerator; | ||
|
||
/// <summary> | ||
/// Generator of the JsonSchema for AppSettings.json including A specific Umbraco version. | ||
/// Initializes a new instance of the <see cref="UmbracoJsonSchemaGenerator" /> class. | ||
/// </summary> | ||
public class UmbracoJsonSchemaGenerator | ||
public UmbracoJsonSchemaGenerator() | ||
=> _innerGenerator = new JsonSchemaGenerator(new UmbracoJsonSchemaGeneratorSettings()); | ||
|
||
/// <summary> | ||
/// Generates a json representing the JsonSchema for AppSettings.json including A specific Umbraco version.. | ||
/// </summary> | ||
public async Task<string> GenerateMainFile() | ||
{ | ||
private static readonly HttpClient s_client = new (); | ||
private readonly JsonSchemaGenerator _innerGenerator; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UmbracoJsonSchemaGenerator" /> class. | ||
/// </summary> | ||
public UmbracoJsonSchemaGenerator() | ||
=> _innerGenerator = new JsonSchemaGenerator(new UmbracoJsonSchemaGeneratorSettings()); | ||
|
||
/// <summary> | ||
/// Generates a json representing the JsonSchema for AppSettings.json including A specific Umbraco version.. | ||
/// </summary> | ||
public async Task<string> Generate() | ||
{ | ||
JObject umbracoSchema = GenerateUmbracoSchema(); | ||
JObject officialSchema = await GetOfficialAppSettingsSchema(); | ||
JObject officialSchema = await GetOfficialAppSettingsSchema(); | ||
JObject externalFilePoints = GenerateSchemaWithExternalDefinitions(); | ||
|
||
officialSchema.Merge(umbracoSchema); | ||
officialSchema.Merge(externalFilePoints); | ||
|
||
return officialSchema.ToString(); | ||
} | ||
return officialSchema.ToString(); | ||
} | ||
|
||
private async Task<JObject> GetOfficialAppSettingsSchema() | ||
{ | ||
HttpResponseMessage response = await s_client.GetAsync("https://json.schemastore.org/appsettings.json") | ||
.ConfigureAwait(false); | ||
|
||
var result = await response.Content.ReadAsStringAsync(); | ||
/// <summary> | ||
/// Generates the CMS file | ||
/// </summary> | ||
/// <returns></returns> | ||
public Task<string> GenerateCmsFile() | ||
{ | ||
JObject cmsSchema = GenerateUmbracoSchema(); | ||
|
||
return JsonConvert.DeserializeObject<JObject>(result)!; | ||
} | ||
return Task.FromResult(cmsSchema.ToString()); | ||
} | ||
|
||
private JObject GenerateUmbracoSchema() | ||
{ | ||
NJsonSchema.JsonSchema schema = _innerGenerator.Generate(typeof(AppSettings)); | ||
|
||
// TODO: when the "UmbracoPath" setter is removed from "GlobalSettings" (scheduled for V12), remove this line as well | ||
schema.Definitions["UmbracoCmsCoreConfigurationModelsGlobalSettings"]?.Properties?.Remove(nameof(GlobalSettings.UmbracoPath)); | ||
|
||
return JsonConvert.DeserializeObject<JObject>(schema.ToJson())!; | ||
private JObject GenerateSchemaWithExternalDefinitions() | ||
{ | ||
var fileProvider = new EmbeddedFileProvider(GetType().Assembly); | ||
|
||
IFileInfo schema = fileProvider.GetFileInfo("appsettings-schema.json"); | ||
|
||
using (Stream? stream = schema.CreateReadStream()) | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
return JsonConvert.DeserializeObject<JObject>(reader.ReadToEnd())!; | ||
} | ||
} | ||
|
||
private async Task<JObject> GetOfficialAppSettingsSchema() | ||
{ | ||
HttpResponseMessage response = await s_client.GetAsync("https://json.schemastore.org/appsettings.json") | ||
.ConfigureAwait(false); | ||
|
||
var result = await response.Content.ReadAsStringAsync(); | ||
|
||
return JsonConvert.DeserializeObject<JObject>(result)!; | ||
} | ||
|
||
private JObject GenerateUmbracoSchema() | ||
{ | ||
NJsonSchema.JsonSchema schema = _innerGenerator.Generate(typeof(AppSettings)); | ||
|
||
// TODO: when the "UmbracoPath" setter is removed from "GlobalSettings" (scheduled for V12), remove this line as well | ||
schema.Definitions["UmbracoCmsCoreConfigurationModelsGlobalSettings"]?.Properties?.Remove(nameof(GlobalSettings.UmbracoPath));return JsonConvert.DeserializeObject<JObject>(schema.ToJson())!; | ||
} | ||
} |
Oops, something went wrong.