public async Task <IActionResult> Post(
            [FromQuery] string operationIds   = null,
            [FromQuery] string tags           = null,
            [FromQuery] string url            = null,
            [FromQuery] string openApiVersion = null,
            [FromQuery] string title          = "Partial Graph API",
            [FromQuery] OpenApiStyle style    = OpenApiStyle.Plain,
            [FromQuery] string format         = null,
            [FromQuery] string graphVersion   = null,
            [FromQuery] bool forceRefresh     = false)
        {
            try
            {
                OpenApiStyleOptions styleOptions = new OpenApiStyleOptions(style, openApiVersion, graphVersion, format);

                string graphUri = GetVersionUri(styleOptions.GraphVersion);

                if (graphUri == null)
                {
                    return(new BadRequestResult());
                }

                OpenApiDocument source = OpenApiService.ConvertCsdlToOpenApi(styleOptions, Request.Body);

                var predicate = await OpenApiService.CreatePredicate(operationIds, tags, url, source, forceRefresh);

                if (predicate == null)
                {
                    return(new BadRequestResult());
                }

                var subsetOpenApiDocument = OpenApiService.CreateFilteredDocument(source, title, styleOptions, predicate);

                subsetOpenApiDocument = OpenApiService.ApplyStyle(styleOptions, subsetOpenApiDocument);

                var stream = OpenApiService.SerializeOpenApiDocument(subsetOpenApiDocument, styleOptions);
                if (styleOptions.OpenApiFormat == "yaml")
                {
                    return(new FileStreamResult(stream, "application/yaml"));
                }
                else
                {
                    return(new FileStreamResult(stream, "application/json"));
                }
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(new ProblemDetails()
                {
                    Detail = ex.Message
                }));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an OpenAPI document from a CSDL document.
        /// </summary>
        /// <param name="graphDocPath">The file path of the CSDL document location.</param>
        /// <param name="styleOptions">Optional parameter that defines the style
        /// options to be used in formatting the OpenAPI document.</param>
        /// <returns>Instance of an OpenApiDocument</returns>
        private static OpenApiDocument CreateOpenApiDocument(string graphDocPath)
        {
            if (string.IsNullOrEmpty(graphDocPath))
            {
                return(null);
            }

            using StreamReader streamReader = new StreamReader(graphDocPath);
            Stream csdl = streamReader.BaseStream;

            OpenApiDocument document = OpenApiService.ConvertCsdlToOpenApi(csdl);

            return(document);
        }
        public async Task <IActionResult> Post(
            [FromQuery] string operationIds   = null,
            [FromQuery] string tags           = null,
            [FromQuery] string url            = null,
            [FromQuery] string openApiVersion = null,
            [FromQuery] string title          = "Partial Graph API",
            [FromQuery] OpenApiStyle style    = OpenApiStyle.Plain,
            [FromQuery] string format         = null,
            [FromQuery] string graphVersion   = null,
            [FromQuery] bool forceRefresh     = false)
        {
            try
            {
                OpenApiStyleOptions styleOptions = new OpenApiStyleOptions(style, openApiVersion, graphVersion, format);

                string graphUri = GetVersionUri(styleOptions.GraphVersion);

                if (graphUri == null)
                {
                    throw new InvalidOperationException($"Unsupported {nameof(graphVersion)} provided: '{graphVersion}'");
                }

                OpenApiDocument source = OpenApiService.ConvertCsdlToOpenApi(Request.Body);

                var predicate = await OpenApiService.CreatePredicate(operationIds, tags, url, source, forceRefresh);

                var subsetOpenApiDocument = OpenApiService.CreateFilteredDocument(source, title, styleOptions.GraphVersion, predicate);

                subsetOpenApiDocument = OpenApiService.ApplyStyle(styleOptions.Style, subsetOpenApiDocument);

                var stream = OpenApiService.SerializeOpenApiDocument(subsetOpenApiDocument, styleOptions);

                if (styleOptions.OpenApiFormat == "yaml")
                {
                    return(new FileStreamResult(stream, "text/yaml"));
                }
                else
                {
                    return(new FileStreamResult(stream, "application/json"));
                }
            }
            catch (InvalidOperationException ex)
            {
                return(new JsonResult(ex.Message)
                {
                    StatusCode = StatusCodes.Status400BadRequest
                });
            }
            catch (ArgumentException ex)
            {
                return(new JsonResult(ex.Message)
                {
                    StatusCode = StatusCodes.Status404NotFound
                });
            }
            catch (Exception ex)
            {
                return(new JsonResult(ex.Message)
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }