Example #1
0
        public void SerializeReferencedSecuritySchemeAsV3JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""sampleSecurityScheme"": null
}";

            // Act
            // Add dummy start object, value, and end object to allow SerializeAsV3 to output security scheme
            // as property name.
            writer.WriteStartObject();
            ReferencedSecurityScheme.SerializeAsV3(writer);
            writer.WriteNull();
            writer.WriteEndObject();
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #2
0
        private void WriteValueRecursive(OpenApiJsonWriter writer, object value)
        {
            if (value == null ||
                value.GetType().IsPrimitive ||
                value is decimal ||
                value is string ||
                value is DateTimeOffset ||
                value is DateTime)
            {
                writer.WriteValue(value);
            }
            else if (value.GetType().IsGenericType&&
                     (typeof(IDictionary <,>).IsAssignableFrom(value.GetType().GetGenericTypeDefinition()) ||
                      typeof(Dictionary <,>).IsAssignableFrom(value.GetType().GetGenericTypeDefinition())))
            {
                writer.WriteStartObject();
                foreach (var elementValue in (dynamic)(value))
                {
                    writer.WritePropertyName(elementValue.Key);
                    WriteValueRecursive(writer, elementValue.Value);
                }

                writer.WriteEndObject();
            }
            else if (typeof(IEnumerable).IsAssignableFrom(value.GetType()))
            {
                writer.WriteStartArray();
                foreach (var elementValue in (IEnumerable)value)
                {
                    WriteValueRecursive(writer, elementValue);
                }

                writer.WriteEndArray();
            }
        }
Example #3
0
        public async Task SerializeReferencedSecuritySchemeAsV3JsonWorksAsync(bool produceTerseOutput)
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings {
                Terse = produceTerseOutput
            });

            // Act
            // Add dummy start object, value, and end object to allow SerializeAsV3 to output security scheme
            // as property name.
            writer.WriteStartObject();
            ReferencedSecurityScheme.SerializeAsV3(writer);
            writer.WriteNull();
            writer.WriteEndObject();
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            await Verifier.Verify(actual).UseParameters(produceTerseOutput);
        }