示例#1
0
        public void ParseDictionarySchemaShouldSucceed()
        {
            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "dictionarySchema.yaml")))
            {
                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 schema = OpenApiV3Deserializer.LoadSchema(node);

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

                schema.ShouldBeEquivalentTo(
                    new OpenApiSchema
                {
                    Type = "object",
                    AdditionalProperties = new OpenApiSchema
                    {
                        Type = "string"
                    }
                });
            }
        }
示例#2
0
        public void ParsePrimitiveSchemaShouldSucceed()
        {
            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "primitiveSchema.yaml")))
            {
                var yamlStream = new YamlStream();
                yamlStream.Load(new StreamReader(stream));
                var yamlNode = yamlStream.Documents.First().RootNode;

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

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

                // Act
                var schema = OpenApiV3Deserializer.LoadSchema(node);

                // Assert
                diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic());

                schema.Should().BeEquivalentTo(
                    new OpenApiSchema
                {
                    Type   = "string",
                    Format = "email"
                });
            }
        }
示例#3
0
        public void ParseSimpleSchemaShouldSucceed()
        {
            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "simpleSchema.yaml")))
            {
                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 schema = OpenApiV3Deserializer.LoadSchema(node);

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

                schema.ShouldBeEquivalentTo(
                    new OpenApiSchema
                {
                    Type     = "object",
                    Required =
                    {
                        "name"
                    },
                    Properties =
                    {
                        ["name"] = new OpenApiSchema
                        {
                        Type = "string"
                        },
                        ["address"] = new OpenApiSchema
                        {
                        Type = "string"
                        },
                        ["age"] = new OpenApiSchema
                        {
                        Type    = "integer",
                        Format  = "int32",
                        Minimum = 0
                        }
                    },
                    AdditionalPropertiesAllowed = false
                });
            }
        }
示例#4
0
        public void ParseBasicSchemaWithExampleShouldSucceed()
        {
            using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicSchemaWithExample.yaml")))
            {
                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 schema = OpenApiV3Deserializer.LoadSchema(node);

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

                schema.ShouldBeEquivalentTo(
                    new OpenApiSchema
                {
                    Type       = "object",
                    Properties =
                    {
                        ["id"] = new OpenApiSchema
                        {
                        Type   = "integer",
                        Format = "int64"
                        },
                        ["name"] = new OpenApiSchema
                        {
                        Type = "string"
                        }
                    },
                    Required =
                    {
                        "name"
                    },
                    Example = new OpenApiObject
                    {
                        ["name"] = new OpenApiString("Puma"),
                        ["id"]   = new OpenApiInteger(1)
                    }
                });
            }
        }
        /// <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);
        }