Beispiel #1
0
        public void ParseAdvancedCallbackWithReferenceShouldSucceed()
        {
            using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "advancedCallbackWithReference.yaml")))
            {
                // Act
                var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);

                // Assert
                var path = openApiDoc.Paths.First().Value;
                var subscribeOperation = path.Operations[OperationType.Post];

                var callback = subscribeOperation.Callbacks["simpleHook"];

                context.ShouldBeEquivalentTo(new OpenApiDiagnostic());

                callback.ShouldBeEquivalentTo(
                    new OpenApiCallback
                {
                    Reference = new OpenApiReference
                    {
                        Type = ReferenceType.Callback,
                        Id   = "simpleHook",
                    },
                    PathItems =
                    {
                        [RuntimeExpression.Build("$request.body#/url")]
                            = new OpenApiPathItem
                            {
                                Operations                   =
                                {
                                    [OperationType.Post] =
                                        new OpenApiOperation
                                                    {
                                                    RequestBody          = new OpenApiRequestBody
                                                    {
                                                    Content          =
                                                    {
                                                    ["application/json"] = new OpenApiMediaType
                                                    {
                                                    Schema   = new OpenApiSchema
                                                    {
                                                    Type = "object"
                                                    }
                                                    }
                                                    }
                                                    },
                                                    Responses            = new OpenApiResponses
                                                    {
                                                    ["200"] = new OpenApiResponse
                                                    {
                                                    Description  = "Success"
                                                    }
                                                    }
                                                    }
                                }
                            }
                    }
                });
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generate a single <see cref="OpenApiCallback"/> matching the given <see cref="WebHookTrigger"/>
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        protected OpenApiCallback Generate(WebHookTrigger trigger, OperationFilterContext context)
        {
            var schema = context.SchemaGenerator.GenerateSchema(trigger.PayloadType, context.SchemaRepository);
            var result = new OpenApiCallback();

            result.AddPathItem(RuntimeExpression.Build($"{{$request.body#/{PseudoCamelCase(nameof(IWebHook.Callback))}}}"), new OpenApiPathItem
            {
                Operations = new Dictionary <OperationType, OpenApiOperation>
                {
                    [OperationType.Post] = new OpenApiOperation
                    {
                        OperationId = trigger.Id,
                        Description = trigger.Description,
                        Responses   = _responses,
                        Parameters  = _parameters,
                        RequestBody = new OpenApiRequestBody
                        {
                            Required = true,
                            Content  = new Dictionary <string, OpenApiMediaType>
                            {
                                ["application/json"] = new OpenApiMediaType {
                                    Schema = schema
                                }
                            }
                        },
                    }
                }
            });

            return(result);
        }
        public void BuildRuntimeExpressionThrowsNullOrWhiteSpace(string expression)
        {
            // Arrange & Act
            Action test = () => RuntimeExpression.Build(expression);

            // Assert
            Assert.Throws <ArgumentException>("expression", test);
        }
        public void CompositeRuntimeExpressionWithInvalidRuntimeExpressions(string expression, string invalidExpression)
        {
            // Arrange & Act
            Action test = () => RuntimeExpression.Build(expression);

            // Assert
            test.ShouldThrow <OpenApiException>().WithMessage(String.Format(SRResource.RuntimeExpressionHasInvalidFormat, invalidExpression));
        }
        public void CompareStaticRuntimeExpressionWorks(string expression)
        {
            // Arrange & Act
            var runtimeExpression1 = RuntimeExpression.Build(expression);
            var runtimeExpression2 = RuntimeExpression.Build(expression);

            // Assert
            Assert.Same(runtimeExpression1, runtimeExpression2);
        }
        public void BuildRuntimeExpressionTwiceCreatesNewEquivalentInstances(string expression)
        {
            // Arrange & Act
            var runtimeExpression1 = RuntimeExpression.Build(expression);
            var runtimeExpression2 = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotSame(runtimeExpression1, runtimeExpression2);
            Assert.Equal(runtimeExpression1, runtimeExpression2);
        }
        public void BuildRuntimeExpressionThrowsWithDollarPrefix(string expression)
        {
            // Arrange & Act
            Action test = () => RuntimeExpression.Build(expression);

            // Assert
            OpenApiException exception = Assert.Throws <OpenApiException>(test);

            Assert.Equal(String.Format(SRResource.RuntimeExpressionMustBeginWithDollar, expression), exception.Message);
        }
        public void BuildRuntimeExpressionThrowsInvalidFormat(string expression)
        {
            // Arrange & Act
            Action test = () => RuntimeExpression.Build(expression);

            // Assert
            OpenApiException exception = Assert.Throws <OpenApiException>(test);

            Assert.Equal(String.Format(SRResource.RuntimeExpressionHasInvalidFormat, expression), exception.Message);
        }
        public void CompositeRuntimeExpressionContainsInvalidExpressions(string expression)
        {
            // Arrange

            // Act
            Action test = () => RuntimeExpression.Build(expression);

            // Assert
            Assert.Throws <OpenApiException>(test);
        }
        public void ParseBasicCallbackShouldSucceed()
        {
            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicCallback.yaml")))
            {
                // Arrange
                var yamlStream = new YamlStream();
                yamlStream.Load(new StreamReader(stream));
                var yamlNode = yamlStream.Documents.First().RootNode;

                var context    = new ParsingContext();
                var diagnostic = new OpenApiDiagnostic();

                var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);

                // Act
                var callback = OpenApiV3Deserializer.LoadCallback(node);

                // Assert
                diagnostic.ShouldBeEquivalentTo(new OpenApiDiagnostic());

                callback.ShouldBeEquivalentTo(
                    new OpenApiCallback
                {
                    PathItems =
                    {
                        [RuntimeExpression.Build("$request.body#/url")]
                            = new OpenApiPathItem
                            {
                                Operations                  =
                                {
                                    [OperationType.Post] =
                                        new OpenApiOperation
                                            {
                                            RequestBody         = new OpenApiRequestBody
                                            {
                                            Content         =
                                            {
                                            ["application/json"] = null
                                            }
                                            },
                                            Responses           = new OpenApiResponses
                                            {
                                            ["200"] = new OpenApiResponse
                                            {
                                            Description = "Success"
                                            }
                                            }
                                            }
                                }
                            }
                    }
                });
            }
        }
        public void BuildMethodRuntimeExpressionReturnsMethodExpression()
        {
            // Arrange
            string expression = "$method";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var method = Assert.IsType <MethodExpression>(runtimeExpression);

            Assert.Equal(expression, method.Expression);
        }
        public void BuildUrlRuntimeExpressionReturnsUrlExpression()
        {
            // Arrange
            string expression = "$url";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var url = Assert.IsType <UrlExpression>(runtimeExpression);

            Assert.Equal(expression, url.Expression);
        }
        public void BuildStatusCodeRuntimeExpressionReturnsStatusCodeExpression()
        {
            // Arrange
            string expression = "$statusCode";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var statusCode = Assert.IsType <StatusCodeExpression>(runtimeExpression);

            Assert.Equal(expression, statusCode.Expression);
        }
Beispiel #14
0
        /// <summary>
        /// Generate a single <see cref="OpenApiCallback"/> matching the given <see cref="WebHookTrigger"/>
        /// </summary>
        /// <param name="trigger"></param>
        /// <returns></returns>
        protected OpenApiCallback Generate(WebHookTrigger trigger)
        {
            var triggerProperties = new Dictionary <string, OpenApiSchema>(trigger.Template.Properties)
            {
                [PseudoCamelCase(DefaultWebHookSender.TimestampKey)] = new OpenApiSchema {
                    Type = "string", Format = "date-time"
                },
                [PseudoCamelCase(DefaultWebHookSender.TriggerKey)] = new OpenApiSchema {
                    Type = "string"
                },
                [PseudoCamelCase(DefaultWebHookSender.UniqueIdKey)] = new OpenApiSchema {
                    Type = "string", Format = "uuid"
                }
            };

            var result = new OpenApiCallback();

            result.AddPathItem(RuntimeExpression.Build($"{{$request.body#/{PseudoCamelCase(nameof(IWebHook.Callback))}}}"), new OpenApiPathItem
            {
                Operations = new Dictionary <OperationType, OpenApiOperation>
                {
                    [OperationType.Post] = new OpenApiOperation
                    {
                        OperationId = trigger.Id,
                        Description = trigger.Description,
                        Responses   = _responses,
                        Parameters  = _parameters,
                        RequestBody = new OpenApiRequestBody
                        {
                            Required = true,
                            Content  = new Dictionary <string, OpenApiMediaType>
                            {
                                ["application/json"] = new OpenApiMediaType
                                {
                                    Schema = new OpenApiSchema
                                    {
                                        Type       = "object",
                                        Properties = triggerProperties
                                    }
                                }
                            }
                        },
                    }
                }
            });

            return(result);
        }
        private static RuntimeExpressionAnyWrapper LoadRuntimeExpressionAnyWrapper(ParseNode node)
        {
            var value = node.GetScalarValue();

            if (value != null && value.StartsWith("$"))
            {
                return(new RuntimeExpressionAnyWrapper
                {
                    Expression = RuntimeExpression.Build(value)
                });
            }

            return(new RuntimeExpressionAnyWrapper
            {
                Any = node.CreateAny()
            });
        }
        public void CompositeRuntimeExpressionContainsExpression()
        {
            // Arrange
            string expression = "This is a composite expression {$url} yay";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var response = Assert.IsType <CompositeExpression>(runtimeExpression);

            Assert.Equal(expression, response.Expression);

            var compositeExpression = runtimeExpression as CompositeExpression;

            Assert.Single(compositeExpression.ContainedExpressions);
        }
        public void CompositeRuntimeExpressionWithoutRecognizedRuntimeExpressions(string expression)
        {
            // Arrange

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            runtimeExpression.Should().NotBeNull();
            runtimeExpression.Should().BeOfType(typeof(CompositeExpression));
            var response = (CompositeExpression)runtimeExpression;

            response.Expression.Should().Be(expression);

            var compositeExpression = runtimeExpression as CompositeExpression;

            // The whole string is treated as the template without any contained expressions.
            compositeExpression.ContainedExpressions.Should().BeEmpty();
        }
        public void CompositeRuntimeExpressionContainsMultipleExpressions()
        {
            // Arrange
            string expression = "This is a composite expression {$url} yay and {$request.header.foo}";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var response = Assert.IsType <CompositeExpression>(runtimeExpression);

            Assert.Equal(expression, response.Expression);

            var compositeExpression = runtimeExpression as CompositeExpression;

            Assert.Equal(2, compositeExpression.ContainedExpressions.Count);

            Assert.IsType <UrlExpression>(compositeExpression.ContainedExpressions.First());
            Assert.IsType <RequestExpression>(compositeExpression.ContainedExpressions.Last());
        }
        public void CompositeRuntimeExpressionForWebHook()
        {
            // Arrange
            string expression = "http://notificationServer.com?transactionId={$request.body#/id}&email={$request.body#/email}";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var response = Assert.IsType <CompositeExpression>(runtimeExpression);

            Assert.Equal(expression, response.Expression);

            var compositeExpression = runtimeExpression as CompositeExpression;

            Assert.Equal(2, compositeExpression.ContainedExpressions.Count);

            Assert.IsType <RequestExpression>(compositeExpression.ContainedExpressions.First());
            Assert.IsType <RequestExpression>(compositeExpression.ContainedExpressions.Last());
        }
        public void BuildRequestRuntimeExpressionReturnsRequestExpression()
        {
            // Arrange
            string expression = "$request.header.accept";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var request = Assert.IsType <RequestExpression>(runtimeExpression);

            Assert.Equal(expression, request.Expression);

            Assert.NotNull(request.Source);
            Assert.Equal("header.accept", request.Source.Expression);

            var header = Assert.IsType <HeaderExpression>(request.Source);

            Assert.Equal("accept", header.Token);
            Assert.Equal("header.accept", header.Expression);
        }
        public void BuildResponseRuntimeExpressionReturnsResponseExpression()
        {
            // Arrange
            string expression = "$response.body#/status";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var response = Assert.IsType <ResponseExpression>(runtimeExpression);

            Assert.Equal(expression, response.Expression);

            Assert.Equal("body#/status", response.Source.Expression);
            Assert.NotNull(response.Source);

            var body = Assert.IsType <BodyExpression>(response.Source);

            Assert.Equal("/status", body.Fragment);
            Assert.Equal("body#/status", body.Expression);
        }
        public void CompositeRuntimeExpressionWithMultipleRuntimeExpressionsAndFakeBraces()
        {
            // Arrange
            string expression = "This is a composite expression {url} {test} and {} {$url} and {$request.header.foo}";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            runtimeExpression.Should().NotBeNull();
            runtimeExpression.Should().BeOfType(typeof(CompositeExpression));
            var response = (CompositeExpression)runtimeExpression;

            response.Expression.Should().Be(expression);

            var compositeExpression = runtimeExpression as CompositeExpression;

            compositeExpression.ContainedExpressions.ShouldBeEquivalentTo(new List <RuntimeExpression>()
            {
                new UrlExpression(),
                new RequestExpression(new HeaderExpression("foo"))
            });
        }
        public void CompositeRuntimeExpressionContainsMultipleExpressions()
        {
            // Arrange
            string expression = "This is a composite expression {$url} yay and {$request.header.foo}";

            // Act
            var runtimeExpression = RuntimeExpression.Build(expression);

            // Assert
            Assert.NotNull(runtimeExpression);
            var response = Assert.IsType <CompositeExpression>(runtimeExpression);

            Assert.Equal(expression, response.Expression);

            var compositeExpression = runtimeExpression as CompositeExpression;

            Assert.Equal(2, compositeExpression.ContainedExpressions.Count);

            compositeExpression.ContainedExpressions.ShouldBeEquivalentTo(new List <RuntimeExpression>()
            {
                new UrlExpression(),
                new RequestExpression(new HeaderExpression("foo"))
            });
        }
        private static RuntimeExpression LoadRuntimeExpression(ParseNode node)
        {
            var value = node.GetScalarValue();

            return(RuntimeExpression.Build(value));
        }
        public void ParseMultipleCallbacksWithReferenceShouldSucceed()
        {
            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleCallbacksWithReference.yaml")))
            {
                // Act
                var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);

                // Assert
                var path = openApiDoc.Paths.First().Value;
                var subscribeOperation = path.Operations[OperationType.Post];

                diagnostic.ShouldBeEquivalentTo(
                    new OpenApiDiagnostic()
                {
                    SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
                });

                var callback1 = subscribeOperation.Callbacks["simpleHook"];

                callback1.ShouldBeEquivalentTo(
                    new OpenApiCallback
                {
                    PathItems =
                    {
                        [RuntimeExpression.Build("$request.body#/url")] = new OpenApiPathItem {
                            Operations =
                                                                                              {
                                                                                              [OperationType.Post] = new OpenApiOperation()
                                                                                              {
                                                                                              RequestBody = new OpenApiRequestBody
                                                                                              {
                                                                                              Content =
                                                                                              {
                                                                                              ["application/json"] = new OpenApiMediaType
                                                                                              {
                                                                                              Schema = new OpenApiSchema()
                                                                                              {
                                                                                              Type = "object"
                                                                                              }
                                                                                              }
                                                                                              }
                                                                                              },
                                                                                              Responses =
                                                                                              {
                                                                                              ["200"] = new OpenApiResponse
                                                                                              {
                                                                                              Description = "Success"
                                                                                              }
                                                                                              }
                                                                                              }
                                                                                              }
                        }
                    },
                    Reference = new OpenApiReference
                    {
                        Type = ReferenceType.Callback,
                        Id   = "simpleHook",
                    }
                });

                var callback2 = subscribeOperation.Callbacks["callback2"];
                callback2.ShouldBeEquivalentTo(
                    new OpenApiCallback
                {
                    PathItems =
                    {
                        [RuntimeExpression.Build("/simplePath")] = new OpenApiPathItem {
                            Operations =
                                                                                       {
                                                                                       [OperationType.Post] = new OpenApiOperation()
                                                                                       {
                                                                                       RequestBody = new OpenApiRequestBody
                                                                                       {
                                                                                       Description = "Callback 2",
                                                                                       Content     =
                                                                                       {
                                                                                       ["application/json"] = new OpenApiMediaType
                                                                                       {
                                                                                       Schema = new OpenApiSchema()
                                                                                       {
                                                                                       Type = "string"
                                                                                       }
                                                                                       }
                                                                                       }
                                                                                       },
                                                                                       Responses =
                                                                                       {
                                                                                       ["400"] = new OpenApiResponse
                                                                                       {
                                                                                       Description = "Callback Response"
                                                                                       }
                                                                                       }
                                                                                       }
                                                                                       },
                        }
                    }
                });

                var callback3 = subscribeOperation.Callbacks["callback3"];
                callback3.ShouldBeEquivalentTo(
                    new OpenApiCallback
                {
                    PathItems =
                    {
                        [RuntimeExpression.Build(@"http://example.com?transactionId={$request.body#/id}&email={$request.body#/email}")] = new OpenApiPathItem {
                            Operations =
                                                                                                                                                              {
                                                                                                                                                              [OperationType.Post] = new OpenApiOperation()
                                                                                                                                                              {
                                                                                                                                                              RequestBody = new OpenApiRequestBody
                                                                                                                                                              {
                                                                                                                                                              Content =
                                                                                                                                                              {
                                                                                                                                                              ["application/xml"] = new OpenApiMediaType
                                                                                                                                                              {
                                                                                                                                                              Schema = new OpenApiSchema()
                                                                                                                                                              {
                                                                                                                                                              Type = "object"
                                                                                                                                                              }
                                                                                                                                                              }
                                                                                                                                                              }
                                                                                                                                                              },
                                                                                                                                                              Responses =
                                                                                                                                                              {
                                                                                                                                                              ["200"] = new OpenApiResponse
                                                                                                                                                              {
                                                                                                                                                              Description = "Success"
                                                                                                                                                              },
                                                                                                                                                              ["401"] = new OpenApiResponse
                                                                                                                                                              {
                                                                                                                                                              Description = "Unauthorized"
                                                                                                                                                              },
                                                                                                                                                              ["404"] = new OpenApiResponse
                                                                                                                                                              {
                                                                                                                                                              Description = "Not Found"
                                                                                                                                                              }
                                                                                                                                                              }
                                                                                                                                                              }
                                                                                                                                                              }
                        }
                    }
                });
            }
        }