|
| 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