Ejemplo n.º 1
0
        private static void ProcessAnyFields <T>(
            MapNode mapNode,
            T domainObject,
            AnyFieldMap <T> anyFieldMap)
        {
            foreach (var anyFieldName in anyFieldMap.Keys.ToList())
            {
                try
                {
                    mapNode.Context.StartObject(anyFieldName);

                    var convertedOpenApiAny = OpenApiAnyConverter.GetSpecificOpenApiAny(
                        anyFieldMap[anyFieldName].PropertyGetter(domainObject),
                        anyFieldMap[anyFieldName].SchemaGetter(domainObject));

                    anyFieldMap[anyFieldName].PropertySetter(domainObject, convertedOpenApiAny);
                }
                catch (OpenApiException exception)
                {
                    exception.Pointer = mapNode.Context.GetLocation();
                    mapNode.Context.Diagnostic.Errors.Add(new OpenApiError(exception));
                }
                finally
                {
                    mapNode.Context.EndObject();
                }
            }
        }
Ejemplo n.º 2
0
        private static void ProcessAnyListFields <T>(
            MapNode mapNode,
            T domainObject,
            AnyListFieldMap <T> anyListFieldMap)
        {
            foreach (var anyListFieldName in anyListFieldMap.Keys.ToList())
            {
                try
                {
                    var newProperty = new List <IOpenApiAny>();

                    mapNode.Context.StartObject(anyListFieldName);

                    foreach (var propertyElement in anyListFieldMap[anyListFieldName].PropertyGetter(domainObject))
                    {
                        newProperty.Add(
                            OpenApiAnyConverter.GetSpecificOpenApiAny(
                                propertyElement,
                                anyListFieldMap[anyListFieldName].SchemaGetter(domainObject)));
                    }

                    anyListFieldMap[anyListFieldName].PropertySetter(domainObject, newProperty);
                }
                catch (OpenApiException exception)
                {
                    exception.Pointer = mapNode.Context.GetLocation();
                    mapNode.Context.Diagnostic.Errors.Add(new OpenApiError(exception));
                }
                finally
                {
                    mapNode.Context.EndObject();
                }
            }
        }
Ejemplo n.º 3
0
 private static IOpenApiExtension LoadExtension(string name, ParseNode node)
 {
     if (node.Context.ExtensionParsers.TryGetValue(name, out var parser))
     {
         return(parser(
                    OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()),
                    OpenApiSpecVersion.OpenApi3_0));
     }
     else
     {
         return(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()));
     }
 }
Ejemplo n.º 4
0
        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 = OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny())
            });
        }
Ejemplo n.º 5
0
        private static void ProcessAnyMapFields <T, U>(
            MapNode mapNode,
            T domainObject,
            AnyMapFieldMap <T, U> anyMapFieldMap)
        {
            foreach (var anyMapFieldName in anyMapFieldMap.Keys.ToList())
            {
                try
                {
                    var newProperty = new List <IOpenApiAny>();

                    mapNode.Context.StartObject(anyMapFieldName);

                    foreach (var propertyMapElement in anyMapFieldMap[anyMapFieldName].PropertyMapGetter(domainObject))
                    {
                        mapNode.Context.StartObject(propertyMapElement.Key);

                        if (propertyMapElement.Value != null)
                        {
                            var any = anyMapFieldMap[anyMapFieldName].PropertyGetter(propertyMapElement.Value);

                            var newAny = OpenApiAnyConverter.GetSpecificOpenApiAny(
                                any,
                                anyMapFieldMap[anyMapFieldName].SchemaGetter(domainObject));

                            anyMapFieldMap[anyMapFieldName].PropertySetter(propertyMapElement.Value, newAny);
                        }
                    }
                }
                catch (OpenApiException exception)
                {
                    exception.Pointer = mapNode.Context.GetLocation();
                    mapNode.Context.Diagnostic.Errors.Add(new OpenApiError(exception));
                }
                finally
                {
                    mapNode.Context.EndObject();
                }
            }
        }
Ejemplo n.º 6
0
 public static IOpenApiAny LoadAny(ParseNode node)
 {
     return(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()));
 }
        public void ParseNestedObjectAsAnyShouldSucceed()
        {
            var input      = @"
    aString: fooBar
    aInteger: 10
    aArray:
      - 1
      - 2
      - 3
    aNestedArray:
      - aFloat: 1
        aPassword: 1234
        aArray: [abc, def]
        aDictionary:
          arbitraryProperty: 1
          arbitraryProperty2: 2
      - aFloat: 1.6
        aArray: [123]
        aDictionary:
          arbitraryProperty: 1
          arbitraryProperty3: 20
    aObject:
      aDate: 2017-02-03
    aDouble: 2.34
    aDateTime: 2017-01-01
                    ";
            var yamlStream = new YamlStream();

            yamlStream.Load(new StringReader(input));
            var yamlNode = yamlStream.Documents.First().RootNode;

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

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

            var anyMap = node.CreateAny();

            var schema = new OpenApiSchema()
            {
                Type       = "object",
                Properties =
                {
                    ["aString"] = new OpenApiSchema()
                                    {
                                    Type = "string"
                                    },
                    ["aInteger"] = new OpenApiSchema()
                                    {
                                    Type   = "integer",
                                    Format = "int32"
                                    },
                    ["aArray"] = new OpenApiSchema()
                                    {
                                    Type  = "array",
                                    Items = new OpenApiSchema()
                                    {
                                    Type   = "integer",
                                    Format = "int64"
                                    }
                                    },
                    ["aNestedArray"] = new OpenApiSchema()
                                    {
                                    Type  = "array",
                                    Items = new OpenApiSchema()
                                    {
                                    Type       = "object",
                                    Properties =
                                    {
                                    ["aFloat"] = new OpenApiSchema()
                                    {
                                    Type   = "number",
                                    Format = "float"
                                    },
                                    ["aPassword"] = new OpenApiSchema()
                                    {
                                    Type   = "string",
                                    Format = "password"
                                    },
                                    ["aArray"] = new OpenApiSchema()
                                    {
                                    Type  = "array",
                                    Items = new OpenApiSchema()
                                    {
                                    Type = "string",
                                    }
                                    },
                                    ["aDictionary"] = new OpenApiSchema()
                                    {
                                    Type = "object",
                                    AdditionalProperties = new OpenApiSchema()
                                    {
                                    Type   = "integer",
                                    Format = "int64"
                                    }
                                    }
                                    }
                                    }
                                    },
                    ["aObject"] = new OpenApiSchema()
                                    {
                                    Type       = "array",
                                    Properties =
                                    {
                                    ["aDate"] = new OpenApiSchema()
                                    {
                                    Type   = "string",
                                    Format = "date"
                                    }
                                    }
                                    },
                    ["aDouble"] = new OpenApiSchema()
                                    {
                                    Type   = "number",
                                    Format = "double"
                                    },
                    ["aDateTime"] = new OpenApiSchema()
                                    {
                                    Type   = "string",
                                    Format = "date-time"
                                    }
                }
            };

            anyMap = OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap, schema);

            diagnostic.Errors.Should().BeEmpty();

            anyMap.Should().BeEquivalentTo(
                new OpenApiObject
            {
                ["aString"]  = new OpenApiString("fooBar"),
                ["aInteger"] = new OpenApiInteger(10),
                ["aArray"]   = new OpenApiArray()
                {
                    new OpenApiLong(1),
                    new OpenApiLong(2),
                    new OpenApiLong(3),
                },
                ["aNestedArray"] = new OpenApiArray()
                {
                    new OpenApiObject()
                    {
                        ["aFloat"]    = new OpenApiFloat(1),
                        ["aPassword"] = new OpenApiPassword("1234"),
                        ["aArray"]    = new OpenApiArray()
                        {
                            new OpenApiString("abc"),
                            new OpenApiString("def")
                        },
                        ["aDictionary"] = new OpenApiObject()
                        {
                            ["arbitraryProperty"]  = new OpenApiLong(1),
                            ["arbitraryProperty2"] = new OpenApiLong(2),
                        }
                    },
                    new OpenApiObject()
                    {
                        ["aFloat"] = new OpenApiFloat((float)1.6),
                        ["aArray"] = new OpenApiArray()
                        {
                            new OpenApiString("123"),
                        },
                        ["aDictionary"] = new OpenApiObject()
                        {
                            ["arbitraryProperty"]  = new OpenApiLong(1),
                            ["arbitraryProperty3"] = new OpenApiLong(20),
                        }
                    }
                },
                ["aObject"] = new OpenApiObject()
                {
                    ["aDate"] = new OpenApiDate(DateTimeOffset.Parse("2017-02-03", CultureInfo.InvariantCulture).Date)
                },
                ["aDouble"]   = new OpenApiDouble(2.34),
                ["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01", CultureInfo.InvariantCulture))
            });
        }
        public void ParseNestedObjectAsAnyWithoutUsingSchemaShouldSucceed()
        {
            var input      = @"
        aString: fooBar
        aInteger: 10
        aArray:
          - 1
          - 2
          - 3
        aNestedArray:
          - aFloat: 1
            aPassword: 1234
            aArray: [abc, def]
            aDictionary:
              arbitraryProperty: 1
              arbitraryProperty2: 2
          - aFloat: 1.6
            aArray: [123]
            aDictionary:
              arbitraryProperty: 1
              arbitraryProperty3: 20
        aObject:
          aDate: 2017-02-03
        aDouble: 2.34
        aDateTime: 2017-01-01
                        ";
            var yamlStream = new YamlStream();

            yamlStream.Load(new StringReader(input));
            var yamlNode = yamlStream.Documents.First().RootNode;

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

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

            var anyMap = node.CreateAny();

            anyMap = OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap);

            diagnostic.Errors.Should().BeEmpty();

            anyMap.Should().BeEquivalentTo(
                new OpenApiObject
            {
                ["aString"]  = new OpenApiString("fooBar"),
                ["aInteger"] = new OpenApiInteger(10),
                ["aArray"]   = new OpenApiArray()
                {
                    new OpenApiInteger(1),
                    new OpenApiInteger(2),
                    new OpenApiInteger(3),
                },
                ["aNestedArray"] = new OpenApiArray()
                {
                    new OpenApiObject()
                    {
                        ["aFloat"]    = new OpenApiInteger(1),
                        ["aPassword"] = new OpenApiInteger(1234),
                        ["aArray"]    = new OpenApiArray()
                        {
                            new OpenApiString("abc"),
                            new OpenApiString("def")
                        },
                        ["aDictionary"] = new OpenApiObject()
                        {
                            ["arbitraryProperty"]  = new OpenApiInteger(1),
                            ["arbitraryProperty2"] = new OpenApiInteger(2),
                        }
                    },
                    new OpenApiObject()
                    {
                        ["aFloat"] = new OpenApiDouble(1.6),
                        ["aArray"] = new OpenApiArray()
                        {
                            new OpenApiInteger(123),
                        },
                        ["aDictionary"] = new OpenApiObject()
                        {
                            ["arbitraryProperty"]  = new OpenApiInteger(1),
                            ["arbitraryProperty3"] = new OpenApiInteger(20),
                        }
                    }
                },
                ["aObject"] = new OpenApiObject()
                {
                    ["aDate"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-02-03", CultureInfo.InvariantCulture))
                },
                ["aDouble"]   = new OpenApiDouble(2.34),
                ["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01", CultureInfo.InvariantCulture))
            });
        }
        public void ParseObjectAsAnyShouldSucceed()
        {
            var input      = @"
aString: fooBar
aInteger: 10
aDouble: 2.34
aDateTime: 2017-01-01
aDate: 2017-01-02
                ";
            var yamlStream = new YamlStream();

            yamlStream.Load(new StringReader(input));
            var yamlNode = yamlStream.Documents.First().RootNode;

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

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

            var anyMap = node.CreateAny();

            var schema = new OpenApiSchema()
            {
                Type       = "object",
                Properties =
                {
                    ["aString"] = new OpenApiSchema()
                    {
                    Type = "string"
                    },
                    ["aInteger"] = new OpenApiSchema()
                    {
                    Type   = "integer",
                    Format = "int32"
                    },
                    ["aDouble"] = new OpenApiSchema()
                    {
                    Type   = "number",
                    Format = "double"
                    },
                    ["aDateTime"] = new OpenApiSchema()
                    {
                    Type   = "string",
                    Format = "date-time"
                    },
                    ["aDate"] = new OpenApiSchema()
                    {
                    Type   = "string",
                    Format = "date"
                    }
                }
            };

            anyMap = OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap, schema);

            diagnostic.Errors.Should().BeEmpty();

            anyMap.Should().BeEquivalentTo(
                new OpenApiObject
            {
                ["aString"]   = new OpenApiString("fooBar"),
                ["aInteger"]  = new OpenApiInteger(10),
                ["aDouble"]   = new OpenApiDouble(2.34),
                ["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01", CultureInfo.InvariantCulture)),
                ["aDate"]     = new OpenApiDate(DateTimeOffset.Parse("2017-01-02", CultureInfo.InvariantCulture).Date),
            });
        }