Skip to content

Commit 5897c2b

Browse files
committed
Add public method SchemaRepository.ReplaceSchemaId
1 parent c2cfe84 commit 5897c2b

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-

1+
Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository.ReplaceSchemaId(System.Type oldSchemaType, string newSchemaId) -> bool

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SchemaRepository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,22 @@ public OpenApiSchemaReference AddDefinition(string schemaId, OpenApiSchema schem
4242

4343
return new OpenApiSchemaReference(schemaId);
4444
}
45+
46+
public bool ReplaceSchemaId(Type oldSchemaType, string newSchemaId)
47+
{
48+
ArgumentNullException.ThrowIfNull(oldSchemaType);
49+
ArgumentException.ThrowIfNullOrEmpty(newSchemaId);
50+
51+
if (_reservedIds.TryGetValue(oldSchemaType, out string oldSchemaId) &&
52+
oldSchemaId != newSchemaId && Schemas.TryGetValue(oldSchemaId, out var targetSchema))
53+
{
54+
Schemas.Add(newSchemaId, targetSchema);
55+
Schemas.Remove(oldSchemaId);
56+
57+
_reservedIds[oldSchemaType] = newSchemaId;
58+
return true;
59+
}
60+
61+
return false;
62+
}
4563
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Microsoft.OpenApi;
2+
3+
namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
4+
5+
public class SchemaRepositoryTests
6+
{
7+
[Fact]
8+
public void ReplaceSchemaId_ExistingSchema_ReplacesSchemaId()
9+
{
10+
var repository = new SchemaRepository();
11+
var generator = new SchemaGenerator(new(), new JsonSerializerDataContractResolver(new()));
12+
var exampleSchemaReference = (OpenApiSchemaReference)generator.GenerateSchema(typeof(Example), repository);
13+
14+
var oldSchemaId = exampleSchemaReference.Reference.Id!;
15+
var exampleSchema = repository.Schemas[oldSchemaId];
16+
Assert.Contains(oldSchemaId, repository.Schemas);
17+
Assert.Same(exampleSchema, repository.Schemas[oldSchemaId]);
18+
19+
var newSchemaId = "alternateNameForExample";
20+
bool result = repository.ReplaceSchemaId(typeof(Example), newSchemaId);
21+
22+
Assert.True(result);
23+
Assert.DoesNotContain(oldSchemaId, repository.Schemas);
24+
Assert.Contains(newSchemaId, repository.Schemas);
25+
Assert.Same(exampleSchema, repository.Schemas[newSchemaId]);
26+
}
27+
28+
[Fact]
29+
public void ReplaceSchemaId_SameName_DoesNotChange()
30+
{
31+
var repository = new SchemaRepository();
32+
var generator = new SchemaGenerator(new(), new JsonSerializerDataContractResolver(new()));
33+
var exampleSchemaReference = (OpenApiSchemaReference)generator.GenerateSchema(typeof(Example), repository);
34+
35+
var unchangedSchemaId = exampleSchemaReference.Reference.Id!;
36+
var exampleSchema = repository.Schemas[unchangedSchemaId];
37+
Assert.Contains(unchangedSchemaId, repository.Schemas);
38+
Assert.Same(exampleSchema, repository.Schemas[unchangedSchemaId]);
39+
40+
bool result = repository.ReplaceSchemaId(typeof(Example), unchangedSchemaId);
41+
42+
Assert.False(result);
43+
Assert.Contains(unchangedSchemaId, repository.Schemas);
44+
Assert.Same(exampleSchema, repository.Schemas[unchangedSchemaId]);
45+
}
46+
47+
[Fact]
48+
public void ReplaceSchemaId_MissingSchema_DoesNotChange()
49+
{
50+
var repository = new SchemaRepository();
51+
52+
var newSchemaId = "alternateNameForExample";
53+
bool result = repository.ReplaceSchemaId(typeof(Example), newSchemaId);
54+
55+
Assert.False(result);
56+
Assert.DoesNotContain(newSchemaId, repository.Schemas);
57+
}
58+
59+
private sealed class Example
60+
{
61+
public string Value { get; set; }
62+
}
63+
}

0 commit comments

Comments
 (0)