public async Task <IActionResult> Get( [FromQuery] string operationIds = null, [FromQuery] string tags = null, [FromQuery] string openApiVersion = "2", [FromQuery] string title = "Partial Graph API", [FromQuery] OpenApiStyle style = OpenApiStyle.Plain, [FromQuery] string format = "yaml", [FromQuery] string graphVersion = "v1.0", [FromQuery] bool forceRefresh = false) { var predicate = OpenApiService.CreatePredicate(operationIds, tags); if (predicate == null) { return(new BadRequestResult()); } OpenApiDocument source = await OpenApiService.GetGraphOpenApiDocument(graphVersion, forceRefresh); var subsetOpenApiDocument = OpenApiService.CreateFilteredDocument(source, title, graphVersion, predicate); subsetOpenApiDocument = OpenApiService.ApplyStyle(style, subsetOpenApiDocument); var stream = OpenApiService.SerializeOpenApiDocument(subsetOpenApiDocument, openApiVersion, format); return(new FileStreamResult(stream, "application/json")); }
public void FormatPathFunctionsOfStringDataTypesWithSingleQuotationMarks() { // Arrange string operationId_1 = "reports.getTeamsUserActivityCounts"; string operationId_2 = "reports.getTeamsUserActivityUserDetail-a3f1"; OpenApiDocument source = _graphBetaSource; var predicate_1 = OpenApiService.CreatePredicate(operationIds: operationId_1, tags: null, url: null, source: source) .GetAwaiter().GetResult(); var predicate_2 = OpenApiService.CreatePredicate(operationIds: operationId_2, tags: null, url: null, source: source) .GetAwaiter().GetResult(); // Act var subsetOpenApiDocument_1 = OpenApiService.CreateFilteredDocument(source, Title, GraphVersion, predicate_1); var subsetOpenApiDocument_2 = OpenApiService.CreateFilteredDocument(source, Title, GraphVersion, predicate_2); // Assert Assert.Collection(subsetOpenApiDocument_1.Paths, item => { Assert.Equal("/reports/microsoft.graph.getTeamsUserActivityCounts(period='{period}')", item.Key); }); Assert.Collection(subsetOpenApiDocument_2.Paths, item => { Assert.Equal("/reports/microsoft.graph.getTeamsUserActivityUserDetail(date={date})", item.Key); }); }
public void ReturnOpenApiDocumentInCreateFilteredDocumentWhenValidArgumentsAreSpecified(string operationIds, string tags, string url) { // Arrange OpenApiDocument source = _graphBetaSource; // Act var predicate = OpenApiService.CreatePredicate(operationIds: operationIds, tags: tags, url: url, source: source) .GetAwaiter().GetResult(); var subsetOpenApiDocument = OpenApiService.CreateFilteredDocument(source, Title, GraphVersion, predicate); // Assert Assert.NotNull(subsetOpenApiDocument); if (!string.IsNullOrEmpty(operationIds)) { Assert.Single(subsetOpenApiDocument.Paths); } else if (!string.IsNullOrEmpty(tags)) { Assert.Equal(2, subsetOpenApiDocument.Paths.Count); } else // url { Assert.Single(subsetOpenApiDocument.Paths); } }
public void FormatPathFunctionsOfStringDataTypesWithSingleQuotationMarks() { // Arrange string operationId_1 = "reports.getTeamsUserActivityCounts"; string operationId_2 = "reports.getTeamsUserActivityUserDetail-a3f1"; string graphVersion = "beta"; OpenApiDocument source = _source; OpenApiStyleOptions styleOptions = new OpenApiStyleOptions(OpenApiStyle.PowerShell, graphVersion: graphVersion); var predicate_1 = OpenApiService.CreatePredicate(operationId_1, null, null, source, false).GetAwaiter().GetResult(); var predicate_2 = OpenApiService.CreatePredicate(operationId_2, null, null, source, false).GetAwaiter().GetResult(); // Act var subsetOpenApiDocument_1 = OpenApiService.CreateFilteredDocument(source, Title, styleOptions, predicate_1); var subsetOpenApiDocument_2 = OpenApiService.CreateFilteredDocument(source, Title, styleOptions, predicate_2); // Assert Assert.Collection(subsetOpenApiDocument_1.Paths, item => { Assert.Equal("/reports/microsoft.graph.getTeamsUserActivityCounts(period='{period}')", item.Key); }); Assert.Collection(subsetOpenApiDocument_2.Paths, item => { Assert.Equal("/reports/microsoft.graph.getTeamsUserActivityUserDetail(date={date})", item.Key); }); }
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 })); } }
public void ThrowsArgumentExceptionInCreateFilteredDocumentWhenNonExistentOperationIdsAndTagsAreSupplied(string operationIds, string tags) { // Arrange OpenApiDocument source = _graphBetaSource; var predicate = OpenApiService.CreatePredicate(operationIds: operationIds, tags: tags, url: null, source: source) .GetAwaiter().GetResult(); // Act & Assert var message = Assert.Throws <ArgumentException>(() => OpenApiService.CreateFilteredDocument(source, Title, GraphVersion, predicate)).Message; Assert.Equal("No paths found for the supplied parameters.", message); }
public void ThrowsArgumentExceptionInApplyStyleWhenNoPathsAreReturned() { // Arrange OpenApiDocument source = _graphBetaSource; var predicate = OpenApiService.CreatePredicate(operationIds: null, tags: null, url: "/", source: source) .GetAwaiter().GetResult(); // root path will be non-existent in a PowerShell styled doc. var subsetOpenApiDocument = OpenApiService.CreateFilteredDocument(source, Title, GraphVersion, predicate); // Act & Assert var message = Assert.Throws <ArgumentException>(() => OpenApiService.ApplyStyle(OpenApiStyle.PowerShell, subsetOpenApiDocument)).Message; Assert.Equal("No paths found for the supplied parameters.", message); }
public void RemoveRootPathFromOpenApiDocumentInApplyStyleForPowerShellOpenApiStyle() { // Arrange OpenApiDocument source = _graphBetaSource; // Act var predicate = OpenApiService.CreatePredicate(operationIds: "*", tags: null, url: null, source: source) .GetAwaiter().GetResult(); // fetch all paths/operations var subsetOpenApiDocument = OpenApiService.CreateFilteredDocument(source, Title, GraphVersion, predicate); subsetOpenApiDocument = OpenApiService.ApplyStyle(OpenApiStyle.PowerShell, subsetOpenApiDocument); // Assert Assert.False(subsetOpenApiDocument.Paths.ContainsKey("/")); // root path }
public async Task <IActionResult> Get( [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 = await OpenApiService.GetGraphOpenApiDocumentAsync(graphUri, forceRefresh, styleOptions); 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); return(new FileStreamResult(stream, "application/json")); } catch { return(new BadRequestResult()); } }
public void EscapePoundCharacterFromNetworkInterfaceSchemaDescription() { // Arrange OpenApiDocument source = _graphBetaSource; var expectedDescription = "Description of the NIC (e.g. Ethernet adapter, Wireless LAN adapter Local Area Connection <#/>, etc.)."; // Act var predicate = OpenApiService.CreatePredicate(operationIds: null, tags: null, url: "/security/hostSecurityProfiles", source: source) .GetAwaiter().GetResult(); var subsetOpenApiDocument = OpenApiService.CreateFilteredDocument(source, Title, GraphVersion, predicate); subsetOpenApiDocument = OpenApiService.ApplyStyle(OpenApiStyle.PowerShell, subsetOpenApiDocument); var parentSchema = subsetOpenApiDocument.Components.Schemas["microsoft.graph.networkInterface"]; var descriptionSchema = parentSchema.Properties["description"]; // Assert Assert.Equal(expectedDescription, descriptionSchema.Description); }
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 }); } }
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); } } } }