Skip to content
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
2 changes: 1 addition & 1 deletion package-versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<DemystifierFrozenVersion>0.4.1</DemystifierFrozenVersion>
<HumanizerFrozenVersion>2.14.1</HumanizerFrozenVersion>
<NewtonsoftJsonFrozenVersion>13.0.4</NewtonsoftJsonFrozenVersion>
<SwashbuckleFrozenVersion>10.0.1</SwashbuckleFrozenVersion>
<SwashbuckleFrozenVersion>10.1.0</SwashbuckleFrozenVersion>
<SystemTextRegularExpressionsFrozenVersion>4.3.1</SystemTextRegularExpressionsFrozenVersion>

<!-- Non-published dependencies (these are safe to update, won't cause a breaking change) -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace JsonApiDotNetCore.OpenApi.Swashbuckle;

internal static class SchemaRepositoryExtensions
{
private const string ReservedIdsFieldName = "_reservedIds";
private static readonly FieldInfo ReservedIdsField = GetReservedIdsField();

private static FieldInfo GetReservedIdsField()
{
FieldInfo? field = typeof(SchemaRepository).GetField(ReservedIdsFieldName, BindingFlags.Instance | BindingFlags.NonPublic);

if (field == null)
{
throw new InvalidOperationException($"Failed to locate private field '{ReservedIdsFieldName}' " +
$"in type '{typeof(SchemaRepository).FullName}' in assembly '{typeof(SchemaRepository).Assembly.FullName}'.");
}

if (field.FieldType != typeof(Dictionary<Type, string>))
{
throw new InvalidOperationException($"Unexpected type '{field.FieldType}' of private field '{ReservedIdsFieldName}' " +
$"in type '{typeof(SchemaRepository).FullName}' in assembly '{typeof(SchemaRepository).Assembly.FullName}'.");
}

return field;
}

public static OpenApiSchemaReference LookupByType(this SchemaRepository schemaRepository, Type schemaType)
{
ArgumentNullException.ThrowIfNull(schemaRepository);
Expand All @@ -48,24 +25,4 @@ public static bool TryLookupByTypeSafe(this SchemaRepository schemaRepository, T
referenceSchema = result ? obliviousReferenceSchema : null;
return result;
}

public static void ReplaceSchemaId(this SchemaRepository schemaRepository, Type oldSchemaType, string newSchemaId)
{
ArgumentNullException.ThrowIfNull(schemaRepository);
ArgumentNullException.ThrowIfNull(oldSchemaType);
ArgumentException.ThrowIfNullOrEmpty(newSchemaId);

if (schemaRepository.TryLookupByTypeSafe(oldSchemaType, out OpenApiSchemaReference? referenceSchema))
{
string oldSchemaId = referenceSchema.GetReferenceId();

IOpenApiSchema targetSchema = schemaRepository.Schemas[oldSchemaId];

schemaRepository.Schemas.Remove(oldSchemaId);
schemaRepository.Schemas.Add(newSchemaId, targetSchema);

var reservedIds = (Dictionary<Type, string>)ReservedIdsField.GetValue(schemaRepository)!;
reservedIds.Remove(oldSchemaType);
}
}
}