Example #1
0
        public void SerializeAdvancedRequestBodyAsV3JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""description"": ""description"",
  ""content"": {
    ""application/json"": {
      ""schema"": {
        ""type"": ""string""
      }
    }
  },
  ""required"": true
}";

            // Act
            AdvancedRequestBody.SerializeAsV3(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #2
0
        public void SerializeReferencedSchemaAsV3WithoutReferenceJsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);

            var expected = @"{
  ""title"": ""title1"",
  ""multipleOf"": 3,
  ""maximum"": 42,
  ""minimum"": 10,
  ""exclusiveMinimum"": true,
  ""type"": ""integer"",
  ""default"": 15,
  ""nullable"": true,
  ""externalDocs"": {
    ""url"": ""http://example.com/externalDocs""
  }
}";

            // Act
            ReferencedSchema.SerializeAsV3WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
        public void SerializeAdvancedLinkAsV3JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""operationId"": ""operationId1"",
  ""parameters"": {
    ""parameter1"": ""$request.path.id""
  },
  ""requestBody"": {
    ""property1"": true
  },
  ""description"": ""description1"",
  ""server"": {
    ""description"": ""serverDescription1""
  }
}";

            // Act
            AdvancedLink.SerializeAsV3(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #4
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);
        }
        public void SerializeParameterWithFormStyleAndExplodeTrueWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""name"": ""name1"",
  ""in"": ""query"",
  ""description"": ""description1"",
  ""style"": ""form"",
  ""schema"": {
    ""type"": ""array"",
    ""items"": {
      ""enum"": [
        ""value1"",
        ""value2""
      ]
    }
  }
}";

            // Act
            ParameterWithFormStyleAndExplodeTrue.SerializeAsV3WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
        public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues, bool produceTerseOutput)
        {
            // Arrange
            var outputString = new StringWriter(CultureInfo.InvariantCulture);
            var writer       = new OpenApiJsonWriter(outputString, new OpenApiJsonWriterSettings {
                Terse = produceTerseOutput
            });

            // Act
            writer.WriteStartArray();
            foreach (var stringValue in stringValues)
            {
                writer.WriteValue(stringValue);
            }

            writer.WriteEndArray();
            writer.Flush();

            var parsedObject   = JsonConvert.DeserializeObject(outputString.GetStringBuilder().ToString());
            var expectedObject =
                JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new List <string>(stringValues)));

            // Assert
            parsedObject.Should().BeEquivalentTo(expectedObject);
        }
Example #7
0
        public void SerializeAdvancedTagAsV2JsonWithoutReferenceWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""name"": ""pet"",
  ""description"": ""Pets operations"",
  ""externalDocs"": {
    ""description"": ""Find more info here"",
    ""url"": ""https://example.com""
  },
  ""x-tag-extension"": null
}";

            // Act
            AdvancedTag.SerializeAsV2WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #8
0
        public async Task Get()
        {
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);

            GetDoc().SerializeAsV3(writer);
            writer.Flush();
            await HttpContext.SendStringAsync(outputStringWriter.GetStringBuilder().ToString(), "application/json", System.Text.Encoding.UTF8);
        }
Example #9
0
        public void SerializeSchemaWRequiredPropertiesAsV2JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           = @"{
  ""title"": ""title1"",
  ""required"": [
    ""property1""
  ],
  ""properties"": {
    ""property1"": {
      ""required"": [
        ""property3""
      ],
      ""properties"": {
        ""property2"": {
          ""type"": ""integer""
        },
        ""property3"": {
          ""maxLength"": 15,
          ""type"": ""string""
        }
      }
    },
    ""property4"": {
      ""properties"": {
        ""property5"": {
          ""properties"": {
            ""property6"": {
              ""type"": ""boolean""
            }
          }
        },
        ""property7"": {
          ""minLength"": 2,
          ""type"": ""string""
        }
      },
      ""readOnly"": true
    }
  },
  ""externalDocs"": {
    ""url"": ""http://example.com/externalDocs""
  }
}";

            // Act
            AdvancedSchemaWithRequiredPropertiesObject.SerializeAsV2(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #10
0
        public static string ConvertToJson(this OpenApiDocument document)
        {
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);

            document.SerializeAsV3(writer);
            writer.Flush();
            return(outputStringWriter.GetStringBuilder().ToString());
        }
Example #11
0
        private static string Write(Action <OpenApiJsonWriter> action)
        {
            MemoryStream      stream = new MemoryStream();
            OpenApiJsonWriter writer = new OpenApiJsonWriter(new StreamWriter(stream));

            action(writer);

            writer.Flush();
            stream.Position = 0;
            return(new StreamReader(stream).ReadToEnd());
        }
        /// <summary>
        /// Serializes the Api document as json (v3)
        /// </summary>
        /// <param name="document">The document to be serialized as json</param>
        /// <returns></returns>
        public static string ToJson(this OpenApiDocument document)
        {
            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                var apiWriter = new OpenApiJsonWriter(writer);
                document.SerializeAsV3(apiWriter);
                apiWriter.Flush();
                writer.Flush();
            }

            return(sb.ToString());
        }
        private HttpContent GetDocumentAsHttpContent(OpenApiDocument document)
        {
            var stream = new MemoryStream();

            using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true))
            {
                var apiWriter = new OpenApiJsonWriter(writer);
                document.SerializeAsV3(apiWriter);
                apiWriter.Flush();
                writer.Flush();
            }

            stream.Position = 0;
            return(new StreamContent(stream));
        }
        public async Task SerializeReferencedRequestBodyAsV3JsonWorksAsync(bool produceTerseOutput)
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings {
                Terse = produceTerseOutput
            });

            // Act
            ReferencedRequestBody.SerializeAsV3(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            await Verifier.Verify(actual).UseParameters(produceTerseOutput);
        }
        /// <summary>
        /// Returns the built document as serialized (V3) string
        /// </summary>
        /// <returns></returns>
        public string BuildAsString()
        {
            var document = Build();

            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                var apiWriter = new OpenApiJsonWriter(writer);
                document.SerializeAsV3(apiWriter);
                apiWriter.Flush();
                writer.Flush();
            }

            return(sb.ToString());
        }
Example #16
0
        public async Task SerializeParameterWithSchemaTypeObjectAsV2JsonWorksAsync(bool produceTerseOutput)
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings {
                Terse = produceTerseOutput
            });

            // Act
            AdvancedHeaderParameterWithSchemaTypeObject.SerializeAsV2(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            await Verifier.Verify(actual).UseParameters(produceTerseOutput);
        }
        /// <summary>
        /// Returns the built document as serialized (V3) HttpContent
        /// </summary>
        /// <returns></returns>
        public HttpContent BuildAsContent()
        {
            var document = Build();

            var stream = new MemoryStream();

            using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true))
            {
                var apiWriter = new OpenApiJsonWriter(writer);
                document.SerializeAsV3(apiWriter);
                apiWriter.Flush();
                writer.Flush();
            }

            return(new ByteArrayContent(stream.ToArray()));
        }
Example #18
0
        public async Task SerializeParameterWithFormStyleAndExplodeTrueWorksAsync(bool produceTerseOutput)
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings {
                Terse = produceTerseOutput
            });

            // Act
            ParameterWithFormStyleAndExplodeTrue.SerializeAsV3WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            await Verifier.Verify(actual).UseParameters(produceTerseOutput);
        }
Example #19
0
        public void SerializeBasicTagAsV2JsonWithoutReferenceWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           = "{ }";

            // Act
            BasicTag.SerializeAsV2WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #20
0
        public void SerializeAdvancedExampleAsV3JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""value"": {
    ""versions"": [
      {
        ""status"": ""Status1"",
        ""id"": ""v1"",
        ""links"": [
          {
            ""href"": ""http://example.com/1"",
            ""rel"": ""sampleRel1"",
            ""bytes"": ""AQID"",
            ""binary"": ""Ñ😻😑♮Í☛oƞ♑😲☇éNjžŁ♻😟¥a´Ī♃ƠąøƩ""
          }
        ]
      },
      {
        ""status"": ""Status2"",
        ""id"": ""v2"",
        ""links"": [
          {
            ""href"": ""http://example.com/2"",
            ""rel"": ""sampleRel2""
          }
        ]
      }
    ]
  }
}";

            // Act
            AdvancedExample.SerializeAsV3(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #21
0
        public void SerializeAdvancedTagAsV2JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);

            var expected = @"""pet""";

            // Act
            AdvancedTag.SerializeAsV2(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
        public void SerializeReferencedResponseAsV3JsonWithoutReferenceWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""description"": ""A complex object array response"",
  ""headers"": {
    ""X-Rate-Limit-Limit"": {
      ""description"": ""The number of allowed requests in the current period"",
      ""schema"": {
        ""type"": ""integer""
      }
    },
    ""X-Rate-Limit-Reset"": {
      ""description"": ""The number of seconds left in the current period"",
      ""schema"": {
        ""type"": ""integer""
      }
    }
  },
  ""content"": {
    ""text/plain"": {
      ""schema"": {
        ""type"": ""array"",
        ""items"": {
          ""$ref"": ""#/components/schemas/customType""
        }
      }
    }
  }
}";

            // Act
            ReferencedResponse.SerializeAsV3WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #23
0
        public void SerializeReferencedExampleAsV3JsonWithoutReferenceWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""value"": {
    ""versions"": [
      {
        ""status"": ""Status1"",
        ""id"": ""v1"",
        ""links"": [
          {
            ""href"": ""http://example.com/1"",
            ""rel"": ""sampleRel1""
          }
        ]
      },
      {
        ""status"": ""Status2"",
        ""id"": ""v2"",
        ""links"": [
          {
            ""href"": ""http://example.com/2"",
            ""rel"": ""sampleRel2""
          }
        ]
      }
    ]
  }
}";

            // Act
            ReferencedExample.SerializeAsV3WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #24
0
        public void SerializeReferencedSchemaAsV3JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);

            var expected = @"{
  ""$ref"": ""#/components/schemas/schemaObject1""
}";

            // Act
            ReferencedSchema.SerializeAsV3(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
        public void SerializeReferencedResponseAsV2JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""$ref"": ""#/responses/example1""
}";

            // Act
            ReferencedResponse.SerializeAsV2(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
        private static string WriteAsJson(IOpenApiAny any)
        {
            // Arrange (continued)
            var            stream = new MemoryStream();
            IOpenApiWriter writer = new OpenApiJsonWriter(new StreamWriter(stream));

            writer.WriteAny(any);
            writer.Flush();
            stream.Position = 0;

            // Act
            var value = new StreamReader(stream).ReadToEnd();

            if (any.AnyType == AnyType.Primitive || any.AnyType == AnyType.Null)
            {
                return(value);
            }

            return(value.MakeLineBreaksEnvironmentNeutral());
        }
        public void SerializeReferencedRequestBodyAsV3JsonWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""$ref"": ""#/components/requestBodies/example1""
}";

            // Act
            ReferencedRequestBody.SerializeAsV3(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #28
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);
        }
        public void SerializeReferencedParameterAsV2JsonWithoutReferenceWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter();
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""in"": ""path"",
  ""name"": ""name1""
}";

            // Act
            ReferencedParameter.SerializeAsV2WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }
Example #30
0
        public void SerializeReferencedSecuritySchemeAsV3JsonWithoutReferenceWorks()
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiJsonWriter(outputStringWriter);
            var expected           =
                @"{
  ""type"": ""openIdConnect"",
  ""description"": ""description1"",
  ""openIdConnectUrl"": ""https://example.com/openIdConnect""
}";

            // Act
            ReferencedSecurityScheme.SerializeAsV3WithoutReference(writer);
            writer.Flush();
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual   = actual.MakeLineBreaksEnvironmentNeutral();
            expected = expected.MakeLineBreaksEnvironmentNeutral();
            actual.Should().Be(expected);
        }