-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4748148
commit 2253ae2
Showing
6 changed files
with
1,107 additions
and
833 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
53 changes: 53 additions & 0 deletions
53
generator/JSONSchemaGenerator/Refiners/ObsoleteEnumRefiner.cs
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Json.Schema.Generation; | ||
using Json.Schema.Generation.Intents; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace JSONSchemaGenerator.Refiners; | ||
|
||
/// <summary> | ||
/// Removes Obsolete Enum values from an EnumIntent. | ||
/// </summary> | ||
/// <remarks>The standard generator will output obsolete values for enums. This makes sense from a "schema" perspective as | ||
/// these items still exist and are therefore valid for the schema. But for documentation reasons, we should remove them.</remarks> | ||
/// TODO: Would https://json-everything.net/ be interested in using this? | ||
/// Based on work in: https://github.com/DW2MC/DW2ModLoader/blob/main/ModDevToolsMod/Dw2ContentDefinitionSchemaRefiner.cs and | ||
/// https://github.com/LicoGame/Magus/blob/main/src/Magus.Json/MagusRefiner.cs without which I would have no idea how to do this. | ||
public class ObsoleteEnumRefiner : ISchemaRefiner | ||
{ | ||
public void Run(SchemaGenerationContextBase context) | ||
{ | ||
Type type = context.Type; | ||
|
||
// Shouldn't happen, but better safe then sorry. | ||
if (!type.IsEnum) | ||
{ | ||
return; | ||
} | ||
|
||
EnumIntent enumIntent = context.Intents.OfType<EnumIntent>().First(); | ||
|
||
if (enumIntent == null) | ||
{ | ||
return; | ||
} | ||
|
||
enumIntent.Names = enumIntent.Names.Where((name) => !ShouldFilterEnumName(type, name)).ToList(); | ||
} | ||
|
||
public static Boolean IsObsolete(Type type, String key) | ||
{ | ||
return type.GetField(key)?.GetCustomAttributes(typeof(ObsoleteAttribute), false).Any() ?? false; | ||
} | ||
|
||
public Boolean ShouldFilterEnumName(Type type, String name) | ||
{ | ||
return IsObsolete(type, name); | ||
} | ||
|
||
public Boolean ShouldRun(SchemaGenerationContextBase context) | ||
{ | ||
// Only run on enums | ||
return context.Type.IsEnum; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Json.Schema.Generation; | ||
using Json.Schema.Generation.Intents; | ||
using System; | ||
using System.Collections; | ||
using System.Linq; | ||
|
||
namespace JSONSchemaGenerator.Refiners; | ||
|
||
/// <summary> | ||
/// Gives any JSON Objects, a title property. Filters out most non-JSON Objects. | ||
/// </summary> | ||
public class TitleRefiner : ISchemaRefiner | ||
{ | ||
public void Run(SchemaGenerationContextBase context) | ||
{ | ||
context.Intents.Add(new TitleIntent(context.Type.Name)); | ||
} | ||
|
||
public bool ShouldRun(SchemaGenerationContextBase context) | ||
{ | ||
if (context.Intents.OfType<TitleIntent>().Any()) | ||
return false; | ||
|
||
return !context.Type.IsPrimitive && | ||
context.Type != typeof(string) && | ||
context.Type != typeof(String) && | ||
context.Type.GetInterfaces().All(f => f != typeof(IEnumerable)) && | ||
!context.Type.IsNullableValueType() && | ||
!context.Type.IsNullableNumber(); | ||
} | ||
} |
Oops, something went wrong.