public void Given_OpenApiFormat_When_GetContentType_Invoked_Then_It_Should_Return_Result(OpenApiFormat format, string expected) { var result = EnumExtensions.GetContentType(format); result.Should().Be(expected); }
/// <summary> /// Initializes a new instance of the <see cref="FileOpenApiGenerator"/> class. /// </summary> /// <param name="input">The input.</param> /// <param name="output">The output.</param> /// <param name="target">The output target.</param> public FileOpenApiGenerator(string input, string output, OpenApiFormat format) : base(output, format) { Input = input; }
/// <summary> /// Serialises the <see cref="OpenApiDocument"/> based on the spec version and format. /// </summary> /// <param name="document"><see cref="OpenApiDocument"/> instance.</param> /// <param name="writer"><see cref="TextWriter"/> instance.</param> /// <param name="version"><see cref="OpenApiSpecVersion"/> value.</param> /// <param name="format"><see cref="OpenApiFormat"/> value.</param> public static void Serialise(this OpenApiDocument document, TextWriter writer, OpenApiSpecVersion version, OpenApiFormat format) { document.ThrowIfNullOrDefault(); writer.ThrowIfNullOrDefault(); var oaw = OpenApiWriterFactory.CreateInstance(format, writer); switch (version) { case OpenApiSpecVersion.OpenApi2_0: document.SerializeAsV2(oaw); oaw.Flush(); return; case OpenApiSpecVersion.OpenApi3_0: document.SerializeAsV3(oaw); oaw.Flush(); return; default: throw new InvalidOperationException("Invalid Open API version"); } }
public void Given_OpenApiFormatType_When_ToOpenApiFormat_Invoked_Then_It_Should_Return_Result(OpenApiFormatType format, OpenApiFormat expected) { var result = EnumExtensions.ToOpenApiFormat(format); result.Should().Be(expected); }
private string Render(OpenApiSpecVersion version, OpenApiFormat format) { //var serialised = default(string); //using (var sw = new StringWriter()) //{ // this.OpenApiDocument.Serialise(sw, version, format); // serialised = sw.ToString(); //} //return serialised; // This is the interim solution to resolve: // https://github.com/Azure/azure-functions-openapi-extension/issues/365 // // It will be removed when the following issue is resolved: // https://github.com/microsoft/OpenAPI.NET/issues/747 var jserialised = default(string); using (var sw = new StringWriter()) { this.OpenApiDocument.Serialise(sw, version, OpenApiFormat.Json); jserialised = sw.ToString(); } var yserialised = default(string); using (var sw = new StringWriter()) { this.OpenApiDocument.Serialise(sw, version, OpenApiFormat.Yaml); yserialised = sw.ToString(); } if (version != OpenApiSpecVersion.OpenApi2_0) { return(format == OpenApiFormat.Json ? jserialised : yserialised); } var jo = JsonConvert.DeserializeObject <JObject>(jserialised); var jts = jo.DescendantsAndSelf() .Where(p => p.Type == JTokenType.Property && (p as JProperty).Name == "parameters") .SelectMany(p => p.Values <JArray>().SelectMany(q => q.Children <JObject>())) .Where(p => p.Value <string>("in") == null) .Where(p => p.Value <string>("description") != null) .Where(p => p.Value <string>("description").Contains("[formData]")) .ToList(); foreach (var jt in jts) { jt["in"] = "formData"; jt["description"] = jt.Value <string>("description").Replace("[formData]", string.Empty); } var serialised = JsonConvert.SerializeObject(jo, Formatting.Indented); if (format == OpenApiFormat.Json) { return(serialised); } var converter = new ExpandoObjectConverter(); var deserialised = JsonConvert.DeserializeObject <ExpandoObject>(serialised, converter); serialised = new SerializerBuilder().Build().Serialize(deserialised); return(serialised); }
/// <summary> /// Initializes a new instance of the <see cref="OpenApiGenerator"/> class. /// </summary> /// <param name="input">The input.</param> /// <param name="output">The output.</param> /// <param name="target">The output target.</param> public OpenApiGenerator(string output, OpenApiFormat format, OpenApiSpecVersion version) { Output = output; Format = format; Version = version; }
public void Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string format, OpenApiFormat expected) { var context = new OpenApiHttpTriggerContext(); var result = context.GetOpenApiFormat(format); result.Should().Be(expected); }
/// <summary> /// Initializes a new instance of the <see cref="UrlOpenApiGenerator"/> class. /// </summary> /// <param name="input">The input.</param> /// <param name="output">The output.</param> /// <param name="target">The output target.</param> public UrlOpenApiGenerator(Uri input, string output, OpenApiFormat format) : base(output, format) { Input = input; }
/// <summary> /// Converts the input RAML file to an Open API Specification output string using the provided options. /// </summary> /// <param name="inputPath">The path to the RAML file.</param> /// <param name="specVersion">The Open API specification version.</param> /// <param name="format">Open API document format.</param> public string Convert(string inputPath, OpenApiSpecVersion specVersion = OpenApiSpecVersion.OpenApi3_0, OpenApiFormat format = OpenApiFormat.Json) { var document = ConvertToOpenApiDocument(inputPath); return(document.Serialize(specVersion, format)); }
/// <summary> /// Converts the input RAML file to an Open API Specification output file using the provided options. /// </summary> /// <param name="inputPath">The path to the RAML file.</param> /// <param name="outputPath">The path to the generated Open API Specification file.</param> /// <param name="specVersion">The Open API specification version.</param> /// <param name="format">Open API document format.</param> public void ConvertToFile(string inputPath, string outputPath, OpenApiSpecVersion specVersion = OpenApiSpecVersion.OpenApi3_0, OpenApiFormat format = OpenApiFormat.Json) { string contents = Convert(inputPath, specVersion, format); File.WriteAllText(outputPath, contents); }
internal byte[] ConvertOpenApiDocument(string inputFileContent, OpenApiSpecVersion apiSpecVersion, OpenApiFormat apiFormat) { using (Stream stream = this.CreateStream(inputFileContent)) { var document = new OpenApiStreamReader().Read(stream, out var context); this.codeGeneratorProgress?.Progress(50, 100); var outputStream = new MemoryStream(); document.Serialize(outputStream, apiSpecVersion, apiFormat); this.codeGeneratorProgress?.Progress(100, 100); var encoding = Encoding.GetEncoding(Encoding.UTF8.WindowsCodePage); //Get the preamble (byte-order mark) for our encoding byte[] preamble = encoding.GetPreamble(); int preambleLength = preamble.Length; outputStream.Position = 0; //Convert the writer contents to a byte array byte[] body = encoding.GetBytes(new StreamReader(outputStream).ReadToEnd()); //Prepend the preamble to body (store result in resized preamble array) Array.Resize(ref preamble, preambleLength + body.Length); Array.Copy(body, 0, preamble, preambleLength, body.Length); //Return the combined byte array return(preamble); } }
public async Task Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string format, OpenApiFormat expected) { var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; var context = new OpenApiHttpTriggerContext(); var result = (await context.SetApplicationAssemblyAsync(location, false)) .GetOpenApiFormat(format); result.Should().Be(expected); }
/// <summary> /// Initializes a new instance of the <see cref="UrlOpenApiGenerator"/> class. /// </summary> /// <param name="input">The input.</param> /// <param name="output">The output.</param> /// <param name="format">The format.</param> /// <param name="settings">Conversion settings.</param> public UrlOpenApiGenerator(Uri input, string output, OpenApiFormat format, OpenApiConvertSettings settings) : base(output, format, settings) { Input = input; }
/// <summary> /// Initializes a new instance of the <see cref="OpenApiGenerator"/> class. /// </summary> /// <param name="output">The output.</param> /// <param name="format">The output format.</param> /// <param name="settings">Conversion settings.</param> public OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSettings settings) { Output = output; Format = format; Settings = settings; }
/// <summary> /// Initializes a new instance of the <see cref="FileOpenApiGenerator"/> class. /// </summary> /// <param name="input">The input.</param> /// <param name="output">The output.</param> /// <param name="target">The output target.</param> public FileOpenApiGenerator(string input, string output, OpenApiFormat format, OpenApiSpecVersion version) : base(output, format, version) { Input = input; }