|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Globalization; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.OpenApi.Tests.Writers |
| 11 | +{ |
| 12 | + public class OpenApiWriterInlineReferenceTests |
| 13 | + { |
| 14 | + private const int InlineReferenceLimit = 128; |
| 15 | + private const int OutputCharacterLimit = 100_000; |
| 16 | + |
| 17 | + [Theory] |
| 18 | + [InlineData(OpenApiSpecVersion.OpenApi2_0, false)] |
| 19 | + [InlineData(OpenApiSpecVersion.OpenApi2_0, true)] |
| 20 | + [InlineData(OpenApiSpecVersion.OpenApi3_0, false)] |
| 21 | + [InlineData(OpenApiSpecVersion.OpenApi3_0, true)] |
| 22 | + [InlineData(OpenApiSpecVersion.OpenApi3_1, false)] |
| 23 | + [InlineData(OpenApiSpecVersion.OpenApi3_1, true)] |
| 24 | + [InlineData(OpenApiSpecVersion.OpenApi3_2, false)] |
| 25 | + [InlineData(OpenApiSpecVersion.OpenApi3_2, true)] |
| 26 | + public void SerializeWithRepeatedInlineReferencesStopsBeforeWriterLimit( |
| 27 | + OpenApiSpecVersion specVersion, |
| 28 | + bool useYaml) |
| 29 | + { |
| 30 | + var readResult = OpenApiDocument.Parse(CreateDocumentWithRepeatedReferences(18), "json"); |
| 31 | + Assert.Empty(readResult.Diagnostic.Errors); |
| 32 | + Assert.NotNull(readResult.Document); |
| 33 | + |
| 34 | + using var textWriter = new CharacterLimitedTextWriter(OutputCharacterLimit); |
| 35 | + IOpenApiWriter writer = useYaml |
| 36 | + ? new OpenApiYamlWriter(textWriter, CreateWriterSettings(InlineReferenceLimit)) |
| 37 | + : new OpenApiJsonWriter(textWriter, CreateWriterSettings(InlineReferenceLimit)); |
| 38 | + |
| 39 | + var exception = Assert.Throws<OpenApiWriterException>( |
| 40 | + () => Serialize(readResult.Document, writer, specVersion)); |
| 41 | + Assert.Contains(InlineReferenceLimit.ToString(CultureInfo.InvariantCulture), exception.Message); |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [InlineData(OpenApiSpecVersion.OpenApi2_0)] |
| 46 | + [InlineData(OpenApiSpecVersion.OpenApi3_0)] |
| 47 | + [InlineData(OpenApiSpecVersion.OpenApi3_1)] |
| 48 | + [InlineData(OpenApiSpecVersion.OpenApi3_2)] |
| 49 | + public void SerializeWithInlineReferencesBelowLimitWorks(OpenApiSpecVersion specVersion) |
| 50 | + { |
| 51 | + var readResult = OpenApiDocument.Parse(CreateDocumentWithRepeatedReferences(4), "json"); |
| 52 | + Assert.Empty(readResult.Diagnostic.Errors); |
| 53 | + Assert.NotNull(readResult.Document); |
| 54 | + |
| 55 | + using var textWriter = new StringWriter(CultureInfo.InvariantCulture); |
| 56 | + var writer = new OpenApiJsonWriter(textWriter, CreateWriterSettings(32)); |
| 57 | + |
| 58 | + Serialize(readResult.Document, writer, specVersion); |
| 59 | + |
| 60 | + Assert.NotEmpty(textWriter.ToString()); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void ReusingSettingsWithNewWriterStartsANewInlineReferenceCount() |
| 65 | + { |
| 66 | + var largeReadResult = OpenApiDocument.Parse(CreateDocumentWithRepeatedReferences(6), "json"); |
| 67 | + Assert.Empty(largeReadResult.Diagnostic.Errors); |
| 68 | + Assert.NotNull(largeReadResult.Document); |
| 69 | + var smallReadResult = OpenApiDocument.Parse(CreateDocumentWithRepeatedReferences(4), "json"); |
| 70 | + Assert.Empty(smallReadResult.Diagnostic.Errors); |
| 71 | + Assert.NotNull(smallReadResult.Document); |
| 72 | + var settings = CreateWriterSettings(32); |
| 73 | + |
| 74 | + using var firstOutput = new StringWriter(CultureInfo.InvariantCulture); |
| 75 | + Assert.Throws<OpenApiWriterException>( |
| 76 | + () => Serialize( |
| 77 | + largeReadResult.Document, |
| 78 | + new OpenApiJsonWriter(firstOutput, settings), |
| 79 | + OpenApiSpecVersion.OpenApi3_0)); |
| 80 | + |
| 81 | + using var secondOutput = new StringWriter(CultureInfo.InvariantCulture); |
| 82 | + Serialize( |
| 83 | + smallReadResult.Document, |
| 84 | + new OpenApiJsonWriter(secondOutput, settings), |
| 85 | + OpenApiSpecVersion.OpenApi3_0); |
| 86 | + |
| 87 | + Assert.DoesNotContain("$ref", secondOutput.ToString()); |
| 88 | + } |
| 89 | + |
| 90 | + private static string CreateDocumentWithRepeatedReferences(int levels) |
| 91 | + { |
| 92 | + var schemas = new StringBuilder(); |
| 93 | + for (var i = 0; i < levels; i++) |
| 94 | + { |
| 95 | + if (i > 0) |
| 96 | + { |
| 97 | + schemas.Append(','); |
| 98 | + } |
| 99 | + |
| 100 | + schemas |
| 101 | + .Append("\"s") |
| 102 | + .Append(i.ToString(CultureInfo.InvariantCulture)) |
| 103 | + .Append("\":{\"allOf\":[{\"$ref\":\"#/components/schemas/s") |
| 104 | + .Append((i + 1).ToString(CultureInfo.InvariantCulture)) |
| 105 | + .Append("\"},{\"$ref\":\"#/components/schemas/s") |
| 106 | + .Append((i + 1).ToString(CultureInfo.InvariantCulture)) |
| 107 | + .Append("\"}]}"); |
| 108 | + } |
| 109 | + |
| 110 | + schemas |
| 111 | + .Append(",\"s") |
| 112 | + .Append(levels.ToString(CultureInfo.InvariantCulture)) |
| 113 | + .Append("\":{\"type\":\"string\"}"); |
| 114 | + |
| 115 | + return $$""" |
| 116 | + { |
| 117 | + "openapi": "3.0.0", |
| 118 | + "info": { |
| 119 | + "title": "Repeated references", |
| 120 | + "version": "1.0.0" |
| 121 | + }, |
| 122 | + "paths": { |
| 123 | + "/test": { |
| 124 | + "get": { |
| 125 | + "responses": { |
| 126 | + "200": { |
| 127 | + "description": "OK", |
| 128 | + "content": { |
| 129 | + "application/json": { |
| 130 | + "schema": { |
| 131 | + "$ref": "#/components/schemas/s0" |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + }, |
| 140 | + "components": { |
| 141 | + "schemas": { |
| 142 | + {{schemas}} |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + """; |
| 147 | + } |
| 148 | + |
| 149 | + private static void Serialize( |
| 150 | + OpenApiDocument document, |
| 151 | + IOpenApiWriter writer, |
| 152 | + OpenApiSpecVersion specVersion) |
| 153 | + { |
| 154 | + switch (specVersion) |
| 155 | + { |
| 156 | + case OpenApiSpecVersion.OpenApi2_0: |
| 157 | + document.SerializeAsV2(writer); |
| 158 | + break; |
| 159 | + case OpenApiSpecVersion.OpenApi3_0: |
| 160 | + document.SerializeAsV3(writer); |
| 161 | + break; |
| 162 | + case OpenApiSpecVersion.OpenApi3_1: |
| 163 | + document.SerializeAsV31(writer); |
| 164 | + break; |
| 165 | + case OpenApiSpecVersion.OpenApi3_2: |
| 166 | + document.SerializeAsV32(writer); |
| 167 | + break; |
| 168 | + default: |
| 169 | + throw new ArgumentOutOfRangeException(nameof(specVersion), specVersion, null); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static OpenApiWriterSettings CreateWriterSettings(int maxInlineReferenceCount) |
| 174 | + { |
| 175 | + return new OpenApiWriterSettings |
| 176 | + { |
| 177 | + InlineLocalReferences = true, |
| 178 | + MaxInlineReferenceCount = maxInlineReferenceCount |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + private sealed class CharacterLimitedTextWriter(int characterLimit) : TextWriter |
| 183 | + { |
| 184 | + private int _characterCount; |
| 185 | + |
| 186 | + public override Encoding Encoding => Encoding.UTF8; |
| 187 | + |
| 188 | + public override void Write(char value) |
| 189 | + { |
| 190 | + CountCharacters(1); |
| 191 | + } |
| 192 | + |
| 193 | + public override void Write(string value) |
| 194 | + { |
| 195 | + CountCharacters(value?.Length ?? 0); |
| 196 | + } |
| 197 | + |
| 198 | + public override void Write(char[] buffer, int index, int count) |
| 199 | + { |
| 200 | + CountCharacters(count); |
| 201 | + } |
| 202 | + |
| 203 | + public override void Write(ReadOnlySpan<char> buffer) |
| 204 | + { |
| 205 | + CountCharacters(buffer.Length); |
| 206 | + } |
| 207 | + |
| 208 | + private void CountCharacters(int count) |
| 209 | + { |
| 210 | + _characterCount += count; |
| 211 | + if (_characterCount > characterLimit) |
| 212 | + { |
| 213 | + throw new CharacterLimitExceededException(); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private sealed class CharacterLimitExceededException : Exception; |
| 219 | + } |
| 220 | +} |
0 commit comments