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
                });
            }
        }
Example #2
0
        public void ReturnStyledOpenApiDocumentInApplyStyleForAllOpenApiStyles(OpenApiStyle style, string url,
                                                                               OperationType operationType, string expectedOperationId = null)
        {
            // Arrange
            OpenApiDocument source = _graphBetaSource;

            // Act
            var predicate = OpenApiService.CreatePredicate(operationIds: null, tags: null, url: url, source: source)
                            .GetAwaiter().GetResult();

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

            subsetOpenApiDocument = OpenApiService.ApplyStyle(style, subsetOpenApiDocument);

            // Assert
            if (style == OpenApiStyle.GEAutocomplete || style == OpenApiStyle.Plain)
            {
                var content = subsetOpenApiDocument.Paths[url]
                              .Operations[operationType]
                              .Responses["200"]
                              .Content;

                Assert.Single(subsetOpenApiDocument.Paths);

                if (style == OpenApiStyle.GEAutocomplete)
                {
                    Assert.Empty(content);
                }
                else // Plain
                {
                    Assert.NotEmpty(content);
                }
            }
            else // PowerShell || PowerPlatform
            {
                if (operationType == OperationType.Post)
                {
                    var anyOf = subsetOpenApiDocument.Paths[url]
                                .Operations[operationType]
                                .Responses["200"]
                                .Content["application/json"]
                                .Schema
                                .AnyOf;

                    Assert.Null(anyOf);
                }

                if (style == OpenApiStyle.PowerShell)
                {
                    if (operationType == OperationType.Post)
                    {
                        var newOperationId = subsetOpenApiDocument.Paths[url]
                                             .Operations[operationType]
                                             .OperationId;

                        Assert.Equal(expectedOperationId, newOperationId);
                    }
                    else if (operationType == OperationType.Patch)
                    {
                        var newOperationId = subsetOpenApiDocument.Paths[url]
                                             .Operations[operationType]
                                             .OperationId;

                        Assert.Equal(expectedOperationId, newOperationId);
                    }
                    else if (operationType == OperationType.Put)
                    {
                        var newOperationId = subsetOpenApiDocument.Paths[url]
                                             .Operations[operationType]
                                             .OperationId;

                        Assert.Equal(expectedOperationId, newOperationId);
                    }
                }
            }
        }