|
| 1 | +using System.Collections.ObjectModel; |
1 | 2 | using ApiCodeGenerator.AsyncApi.DOM; |
| 3 | +using Moq; |
| 4 | +using Newtonsoft.Json.Linq; |
2 | 5 | using NUnit.Framework.Constraints; |
3 | 6 |
|
4 | 7 | namespace ApiCodeGenerator.AsyncApi.Tests; |
@@ -83,6 +86,84 @@ public async Task ParameterNameReplacementOn() |
83 | 86 | Assert.IsInstanceOf<ParameterNameGeneratorWithReplace>(settings.ParameterNameGenerator); |
84 | 87 | } |
85 | 88 |
|
| 89 | + [Test] |
| 90 | + public async Task LoadApiDocument_WithTextPreprocess() |
| 91 | + { |
| 92 | + const string schemaName = nameof(schemaName); |
| 93 | + var settingsJson = new JObject(); |
| 94 | + |
| 95 | + Func<string, string?, string> dlgt = new FakeTextPreprocessor("{}").Process; |
| 96 | + |
| 97 | + var context = CreateContext(settingsJson); |
| 98 | + |
| 99 | + context.Preprocessors = new Preprocessors( |
| 100 | + new Dictionary<Type, Delegate[]> { [typeof(string)] = [dlgt] }); |
| 101 | + |
| 102 | + var gen = (FakeContentGenerator)await FakeContentGenerator.CreateAsync(context); |
| 103 | + |
| 104 | + var apiDocument = gen.Document; |
| 105 | + |
| 106 | + Assert.NotNull(apiDocument); |
| 107 | + Assert.That(apiDocument?.Components?.Schemas, Does.ContainKey(schemaName)); |
| 108 | + var sch = apiDocument?.Components?.Schemas[schemaName].ToJson(Newtonsoft.Json.Formatting.None); |
| 109 | + Assert.That(sch, Is.EqualTo("{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"processed\":{}}")); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public async Task LoadApiDocument_WithTextPreprocess_Log() |
| 114 | + { |
| 115 | + const string schemaName = nameof(schemaName); |
| 116 | + const string filePath = "cd4bed67-1cc0-44a2-8dd1-30a0bd0c1dee"; |
| 117 | + var settingsJson = new JObject(); |
| 118 | + |
| 119 | + Func<string, string?, ILogger?, string> dlgt = new FakeTextPreprocessor("{}").Process; |
| 120 | + |
| 121 | + var logger = new Mock<ILogger>(); |
| 122 | + var context = CreateContext(settingsJson); |
| 123 | + context.Logger = logger.Object; |
| 124 | + context.DocumentPath = filePath; |
| 125 | + |
| 126 | + context.Preprocessors = new Preprocessors( |
| 127 | + new Dictionary<Type, Delegate[]> { [typeof(string)] = [dlgt] }); |
| 128 | + |
| 129 | + var gen = (FakeContentGenerator)await FakeContentGenerator.CreateAsync(context); |
| 130 | + |
| 131 | + var apiDocument = gen.Document; |
| 132 | + |
| 133 | + Assert.NotNull(apiDocument); |
| 134 | + Assert.That(apiDocument?.Components?.Schemas, Does.ContainKey(schemaName)); |
| 135 | + var sch = apiDocument?.Components?.Schemas[schemaName].ToJson(Newtonsoft.Json.Formatting.None); |
| 136 | + Assert.That(sch, Is.EqualTo("{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"processed\":{}}")); |
| 137 | + logger.Verify(l => l.LogWarning(filePath, It.IsAny<string>())); |
| 138 | + } |
| 139 | + |
| 140 | + [Test] |
| 141 | + public async Task LoadApiDocument_WithModelPreprocess() |
| 142 | + { |
| 143 | + const string schemaName = nameof(schemaName); |
| 144 | + var settingsJson = new JObject(); |
| 145 | + |
| 146 | + Func<AsyncApiDocument, string?, AsyncApiDocument> dlgt = new FakeModelPreprocessor("{}").Process; |
| 147 | + |
| 148 | + var context = CreateContext(settingsJson); |
| 149 | + context.DocumentReader = new StringReader("{\"components\":{\"schemas\":{\"" + schemaName + "\":{\"$schema\":\"http://json-schema.org/draft-04/schema#\"}}}}"); |
| 150 | + |
| 151 | + context.Preprocessors = new Preprocessors( |
| 152 | + new Dictionary<Type, Delegate[]> |
| 153 | + { |
| 154 | + [typeof(AsyncApiDocument)] = [dlgt], |
| 155 | + }); |
| 156 | + |
| 157 | + var gen = (FakeContentGenerator)await FakeContentGenerator.CreateAsync(context); |
| 158 | + |
| 159 | + var apiDocument = gen.Document; |
| 160 | + |
| 161 | + Assert.NotNull(apiDocument); |
| 162 | + Assert.That(apiDocument?.Components?.Schemas, Does.ContainKey(schemaName)); |
| 163 | + var sch = apiDocument?.Components?.Schemas[schemaName].ToJson(Newtonsoft.Json.Formatting.None); |
| 164 | + Assert.That(sch, Is.EqualTo("{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"properties\":{\"processedModel\":{}}}")); |
| 165 | + } |
| 166 | + |
86 | 167 | private static Func<Type, Newtonsoft.Json.JsonSerializer?, IReadOnlyDictionary<string, string>?, object?> GetSettingsFactory(string json) |
87 | 168 | => (t, s, v) => (s ?? new()).Deserialize(new StringReader(json), t); |
88 | 169 |
|
@@ -186,4 +267,16 @@ private void ValidateDocument(AsyncApiDocument document) |
186 | 267 | && a.Default == "def" |
187 | 268 | && a.Examples?.FirstOrDefault() == "exam")); |
188 | 269 | } |
| 270 | + |
| 271 | + private GeneratorContext CreateContext(JObject settingsJson, Core.ExtensionManager.Extensions? extension = null) |
| 272 | + { |
| 273 | + extension ??= new(); |
| 274 | + return new GeneratorContext( |
| 275 | + (t, s, _) => settingsJson.ToObject(t, s ?? new()), |
| 276 | + extension, |
| 277 | + new ReadOnlyDictionary<string, string>(new Dictionary<string, string>())) |
| 278 | + { |
| 279 | + DocumentReader = new StringReader("{}"), |
| 280 | + }; |
| 281 | + } |
189 | 282 | } |
0 commit comments