Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unreferenced schema, add --trim-unused-schema & --keep-schema #199

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Refitter.Core/RefitGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ public static async Task<RefitGenerator> CreateAsync(RefitGeneratorSettings sett
ProcessTagFilters(openApiDocument, settings.IncludeTags);
ProcessPathFilters(openApiDocument, settings.IncludePathMatches);

ProcessContractFilter(openApiDocument, settings.TrimUnusedSchema, settings.KeepSchemaPatterns);

return new RefitGenerator(settings, openApiDocument);
}

private static void ProcessContractFilter(OpenApiDocument openApiDocument, bool removeUnusedSchema, string[] includeSchemaMatches)
{
if (!removeUnusedSchema)
{
return;
}
var cleaner = new SchemaCleaner(openApiDocument, includeSchemaMatches);
cleaner.RemoveUnreferencedSchema();
}

private static void ProcessTagFilters(OpenApiDocument document, IReadOnlyCollection<string> includeTags)
{
if (includeTags.Count == 0)
Expand Down
206 changes: 206 additions & 0 deletions src/Refitter.Core/SchemaCleaner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System.Text.RegularExpressions;

using NJsonSchema;

using NSwag;

namespace Refitter.Core;

public class SchemaCleaner
{
private readonly OpenApiDocument document;
private readonly string[] keepSchemaPatterns;

public SchemaCleaner(OpenApiDocument document, string[] keepSchemaPatterns)
{
this.document = document;
this.keepSchemaPatterns = keepSchemaPatterns;
}

public void RemoveUnreferencedSchema()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kirides can you please add an empty line between the constructor and the RemoveUnreferencedSchema() method declaration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{
var usage = FindUsedSchema(document);

var unused = document.Components.Schemas.Where(s => !usage.Contains(s.Key))
.ToArray();

foreach (var unusedSchema in unused)
{
document.Components.Schemas.Remove(unusedSchema);
}
}

HashSet<string> FindUsedSchema(OpenApiDocument doc)
{
var toProcess = new Stack<JsonSchema>();
var schemaIdLookup = document.Components.Schemas
.ToDictionary(x => x.Value,
x => x.Key);

var keepSchemaRegexes = keepSchemaPatterns
.Select(x => new Regex(x, RegexOptions.Compiled))
.ToArray();

if (doc.Components?.Schemas != null)
{
foreach (var kvp in doc.Components.Schemas)
{
var schema = kvp.Key;
if (keepSchemaRegexes.Any(x => x.IsMatch(schema)))
{
TryPush(kvp.Value, toProcess);
}
}
}

foreach (var kvp in doc.Paths)
{
var pathItem = kvp.Value;
foreach (JsonSchema? schema in GetSchemaForPath(pathItem))
{
TryPush(schema, toProcess);
}
}

var seenIds = new HashSet<string>();
var seen = new HashSet<JsonSchema>();
while (toProcess.Count > 0)
{
var schema = toProcess.Pop();
if (!seen.Add(schema.ActualSchema))
{
continue;
}

// NOTE: NSwag schema stuff seems weird, with all their "Actual..."
if (schemaIdLookup.TryGetValue(schema.ActualSchema, out var refId))
{
if (!seenIds.Add(refId))
{
// prevent recursion
continue;
}
}

foreach (var subSchema in EnumerateSchema(schema.ActualSchema))
{
TryPush(subSchema, toProcess);
}
}

return seenIds;
}

IEnumerable<JsonSchema?> GetSchemaForPath(OpenApiPathItem pathItem)
{
foreach (var p in pathItem.Parameters)
{
yield return p;

Check warning on line 98 in src/Refitter.Core/SchemaCleaner.cs

View check run for this annotation

Codecov / codecov/patch

src/Refitter.Core/SchemaCleaner.cs#L98

Added line #L98 was not covered by tests
}

foreach (var op in pathItem.Values)
{
if (op.RequestBody != null)
{
var body = op.RequestBody;
foreach (var kvpBody in body.Content)
{
var content = kvpBody.Value;
yield return content.Schema;
}
}

foreach (var p in op.ActualParameters)
{
yield return p;
}

foreach (var resp in op.ActualResponses.Select(x => x.Value))
{
foreach (var header in resp.Headers.Select(x => x.Value))
{
yield return header;
}

foreach (var mediaType in resp.Content.Select(x => x.Value))
{
yield return mediaType.Schema;
}
}
}
}

private void TryPush(JsonSchema? schema, Stack<JsonSchema> stack)
{
if (schema == null)
{
return;

Check warning on line 137 in src/Refitter.Core/SchemaCleaner.cs

View check run for this annotation

Codecov / codecov/patch

src/Refitter.Core/SchemaCleaner.cs#L137

Added line #L137 was not covered by tests
}

stack.Push(schema);
}

IEnumerable<JsonSchema> EnumerateSchema(JsonSchema? schema)
{
if (schema is null)
{
return Enumerable.Empty<JsonSchema>();

Check warning on line 147 in src/Refitter.Core/SchemaCleaner.cs

View check run for this annotation

Codecov / codecov/patch

src/Refitter.Core/SchemaCleaner.cs#L147

Added line #L147 was not covered by tests
}

return EnumerateInternal(schema)
.Where(x => x != null)
.Select(x => x!);

static IEnumerable<JsonSchema?> EnumerateInternal(JsonSchema schema)
{
// schema = schema.ActualSchema;
yield return schema.AdditionalItemsSchema;
yield return schema.AdditionalPropertiesSchema;
if (schema.AllInheritedSchemas != null)
{
foreach (JsonSchema s in schema.AllInheritedSchemas)
{
yield return s;
}
}

if (schema.Items != null)
{
foreach (JsonSchema s in schema.Items)
{
yield return s;

Check warning on line 171 in src/Refitter.Core/SchemaCleaner.cs

View check run for this annotation

Codecov / codecov/patch

src/Refitter.Core/SchemaCleaner.cs#L171

Added line #L171 was not covered by tests
}
}

yield return schema.Not;


foreach (var subSchema in schema.AllOf)
{
yield return subSchema;
}

foreach (var subSchema in schema.AnyOf)
{
yield return subSchema;

Check warning on line 185 in src/Refitter.Core/SchemaCleaner.cs

View check run for this annotation

Codecov / codecov/patch

src/Refitter.Core/SchemaCleaner.cs#L185

Added line #L185 was not covered by tests
}

foreach (var subSchema in schema.OneOf)
{
yield return subSchema;

Check warning on line 190 in src/Refitter.Core/SchemaCleaner.cs

View check run for this annotation

Codecov / codecov/patch

src/Refitter.Core/SchemaCleaner.cs#L190

Added line #L190 was not covered by tests
}

foreach (var kvp in schema.Properties)
{
var subSchema = kvp.Value;
yield return subSchema;
}

foreach (var kvp in schema.Definitions)
{
var subSchema = kvp.Value;
yield return subSchema;

Check warning on line 202 in src/Refitter.Core/SchemaCleaner.cs

View check run for this annotation

Codecov / codecov/patch

src/Refitter.Core/SchemaCleaner.cs#L201-L202

Added lines #L201 - L202 were not covered by tests
}
}
}
}
12 changes: 12 additions & 0 deletions src/Refitter.Core/Settings/RefitGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,16 @@ public class RefitGeneratorSettings
/// Gets or sets the settings describing how to generate types using NSwag
/// </summary>
public CodeGeneratorSettings? CodeGeneratorSettings { get; set; }

/// <summary>
/// Set to <c>true</c> to apply tree-shaking to the OpenApi schema.
/// This works in conjunction with <see cref="IncludeTags"/> and <see cref="IncludePathMatches"/>.
/// </summary>
public bool TrimUnusedSchema { get; set; }

/// <summary>
/// Array of regular expressions that determine if a schema needs to be kept.
/// This works in conjunction with <see cref="TrimUnusedSchema"/>.
/// </summary>
public string[] KeepSchemaPatterns { get; set; } = Array.Empty<string>();
}
Loading
Loading