コード例 #1
0
        public void ParseQueryParameterShouldSucceed()
        {
            // Arrange
            MapNode node;

            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "queryParameter.yaml")))
            {
                node = TestHelper.CreateYamlMapNode(stream);
            }

            // Act
            var parameter = OpenApiV3Deserializer.LoadParameter(node);

            // Assert
            parameter.ShouldBeEquivalentTo(
                new OpenApiParameter
            {
                In          = ParameterLocation.Query,
                Name        = "id",
                Description = "ID of the object to fetch",
                Required    = false,
                Schema      = new OpenApiSchema
                {
                    Type  = "array",
                    Items = new OpenApiSchema
                    {
                        Type = "string"
                    }
                },
                Style   = ParameterStyle.Form,
                Explode = true
            });
        }
コード例 #2
0
        public void ParseQueryParameterWithObjectTypeShouldSucceed()
        {
            // Arrange
            MapNode node;

            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "queryParameterWithObjectType.yaml")))
            {
                node = TestHelper.CreateYamlMapNode(stream);
            }

            // Act
            var parameter = OpenApiV3Deserializer.LoadParameter(node);

            // Assert
            parameter.ShouldBeEquivalentTo(
                new OpenApiParameter
            {
                In     = ParameterLocation.Query,
                Name   = "freeForm",
                Schema = new OpenApiSchema
                {
                    Type = "object",
                    AdditionalProperties = new OpenApiSchema
                    {
                        Type = "integer"
                    }
                },
                Style = ParameterStyle.Form
            });
        }
コード例 #3
0
        public void ParseParameterWithExampleShouldSucceed()
        {
            // Arrange
            MapNode node;

            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "parameterWithExample.yaml")))
            {
                node = TestHelper.CreateYamlMapNode(stream);
            }

            // Act
            var parameter = OpenApiV3Deserializer.LoadParameter(node);

            // Assert
            parameter.ShouldBeEquivalentTo(
                new OpenApiParameter
            {
                In          = null,
                Name        = "username",
                Description = "username to fetch",
                Required    = true,
                Example     = new OpenApiFloat(5),
                Schema      = new OpenApiSchema
                {
                    Type   = "number",
                    Format = "float"
                }
            });
        }
コード例 #4
0
        public void ParsePathParameterShouldSucceed()
        {
            // Arrange
            MapNode node;

            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "pathParameter.yaml")))
            {
                node = TestHelper.CreateYamlMapNode(stream);
            }

            // Act
            var parameter = OpenApiV3Deserializer.LoadParameter(node);

            // Assert
            parameter.ShouldBeEquivalentTo(
                new OpenApiParameter
            {
                In          = ParameterLocation.Path,
                Name        = "username",
                Description = "username to fetch",
                Required    = true,
                Schema      = new OpenApiSchema
                {
                    Type = "string"
                }
            });
        }
コード例 #5
0
        public void ParseHeaderParameterShouldSucceed()
        {
            // Arrange
            MapNode node;

            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerParameter.yaml")))
            {
                node = TestHelper.CreateYamlMapNode(stream);
            }

            // Act
            var parameter = OpenApiV3Deserializer.LoadParameter(node);

            // Assert
            parameter.ShouldBeEquivalentTo(
                new OpenApiParameter
            {
                In          = ParameterLocation.Header,
                Name        = "token",
                Description = "token to be passed as a header",
                Required    = true,
                Style       = ParameterStyle.Simple,

                Schema = new OpenApiSchema
                {
                    Type  = "array",
                    Items = new OpenApiSchema
                    {
                        Type   = "integer",
                        Format = "int64",
                    }
                }
            });
        }
コード例 #6
0
        public void ParseQueryParameterWithObjectTypeAndContentShouldSucceed()
        {
            // Arrange
            MapNode node;

            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "queryParameterWithObjectTypeAndContent.yaml")))
            {
                node = TestHelper.CreateYamlMapNode(stream);
            }

            // Act
            var parameter = OpenApiV3Deserializer.LoadParameter(node);

            // Assert
            parameter.ShouldBeEquivalentTo(
                new OpenApiParameter
            {
                In      = ParameterLocation.Query,
                Name    = "coordinates",
                Content =
                {
                    ["application/json"] = new OpenApiMediaType
                                {
                                Schema = new OpenApiSchema
                                {
                                Type     = "object",
                                Required =
                                {
                                "lat",
                                "long"
                                },
                                Properties =
                                {
                                ["lat"] = new OpenApiSchema
                                {
                                Type = "number"
                                },
                                ["long"] = new OpenApiSchema
                                {
                                Type = "number"
                                }
                                }
                                }
                                }
                }
            });
        }
コード例 #7
0
        /// <summary>
        /// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
        /// </summary>
        public bool TryLoadReference(OpenApiReference reference, out IOpenApiReferenceable referencedObject)
        {
            referencedObject = null;

            if (reference == null)
            {
                return(false);
            }

            if (reference.IsExternal)
            {
                // TODO: need to read the external document and load the referenced object.
                throw new NotImplementedException(SRResource.LoadReferencedObjectFromExternalNotImplmented);
            }

            if (!reference.Type.HasValue)
            {
                throw new ArgumentException("Local reference must have type specified.");
            }

            // Special case for Tag
            if (reference.Type == ReferenceType.Tag)
            {
                foreach (var tag in _tags)
                {
                    if (tag.Name == reference.Id)
                    {
                        referencedObject = tag;
                        return(true);
                    }
                }

                referencedObject = new OpenApiTag {
                    Name = reference.Id
                };
                return(false);
            }

            var componentJsonPointer =
                new JsonPointer("#/components/" + reference.Type.GetDisplayName() + "/" + reference.Id);

            var node = _rootNode.Find(componentJsonPointer);

            switch (reference.Type)
            {
            case ReferenceType.Schema:
                referencedObject = OpenApiV3Deserializer.LoadSchema(node);
                break;

            case ReferenceType.Response:
                referencedObject = OpenApiV3Deserializer.LoadResponse(node);
                break;

            case ReferenceType.Parameter:
                referencedObject = OpenApiV3Deserializer.LoadParameter(node);
                break;

            case ReferenceType.Example:
                referencedObject = OpenApiV3Deserializer.LoadExample(node);
                break;

            case ReferenceType.RequestBody:
                referencedObject = OpenApiV3Deserializer.LoadRequestBody(node);
                break;

            case ReferenceType.Header:
                referencedObject = OpenApiV3Deserializer.LoadHeader(node);
                break;

            case ReferenceType.SecurityScheme:
                referencedObject = OpenApiV3Deserializer.LoadSecurityScheme(node);
                break;

            case ReferenceType.Link:
                referencedObject = OpenApiV3Deserializer.LoadLink(node);
                break;

            case ReferenceType.Callback:
                referencedObject = OpenApiV3Deserializer.LoadCallback(node);
                break;

            default:
                throw new OpenApiException(
                          string.Format(
                              SRResource.ReferenceV3HasInvalidValue,
                              reference.Type,
                              componentJsonPointer));
            }

            return(true);
        }