コード例 #1
0
        public void TestSerialize_PartialProperties()
        {
            var actual = new OpenApiQueryResult <SimpleClass>
            {
                Options = new OpenApiQueryOptions <SimpleClass>
                {
                    SelectExpand =
                    {
                        RootSelectClause  =
                        {
                            IsStarSelect  = false,
                            SelectClauses = new Dictionary <PropertyInfo, SelectClause>
                            {
                                [typeof(SimpleClass).GetProperty(nameof(SimpleClass.IntProp))]    = new SelectClause(),
                                [typeof(SimpleClass).GetProperty(nameof(SimpleClass.StringProp))] = new SelectClause()
                            }
                        }
                    }
                },
                TotalCount  = 2,
                ResultItems = new[]
                {
                    new SimpleClass
                    {
                        IntProp    = 1,
                        DoubleProp = 47.11,
                        StringProp = "Hello World"
                    },
                    new SimpleClass
                    {
                        IntProp    = 2,
                        DoubleProp = 47.12,
                        StringProp = "Foo Bar"
                    }
                }
            };

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 2,
                ["value"]        = new[]
                {
                    new
                    {
                        intProp    = 1,
                        stringProp = "Hello World"
                    },
                    new
                    {
                        intProp    = 2,
                        stringProp = "Foo Bar"
                    }
                }
            },
                            actual);
        }
コード例 #2
0
        public void TestSerialize_Polymorphic()
        {
            var actual = new OpenApiQueryResult <Base>
            {
                TotalCount  = 2,
                ResultItems = new Base[]
                {
                    new Sub1
                    {
                        BaseProp = 1,
                        Sub1Prop = 47.11,
                        SubProp  = 47
                    },
                    new Sub2
                    {
                        BaseProp = 2,
                        Sub2Prop = "Test",
                        SubProp  = -47
                    }
                }
            };

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 2,
                ["value"]        = new[]
                {
                    new Dictionary <string, object>
                    {
                        ["@odata.type"] = "Sub1",
                        ["baseProp"]    = 1,
                        ["sub1Prop"]    = 47.11,
                        ["subProp"]     = 47
                    },
                    new Dictionary <string, object>
                    {
                        ["@odata.type"] = "Sub2",
                        ["baseProp"]    = 2,
                        ["sub2Prop"]    = "Test",
                        ["subProp"]     = -47
                    }
                }
            },
                            actual);
        }
コード例 #3
0
        public void TestSerialize_Simple()
        {
            var actual = new OpenApiQueryResult <SimpleClass>
            {
                TotalCount  = 2,
                ResultItems = new[]
                {
                    new SimpleClass
                    {
                        IntProp    = 1,
                        DoubleProp = 47.11,
                        StringProp = "Hello World"
                    },
                    new SimpleClass
                    {
                        IntProp    = 2,
                        DoubleProp = 47.12,
                        StringProp = "Foo Bar"
                    }
                }
            };

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 2,
                ["value"]        = new[]
                {
                    new
                    {
                        intProp    = 1,
                        doubleProp = 47.11,
                        stringProp = "Hello World"
                    },
                    new
                    {
                        intProp    = 2,
                        doubleProp = 47.12,
                        stringProp = "Foo Bar"
                    }
                }
            },
                            actual);
        }
コード例 #4
0
        public void TestSerialize_Dictionary_SimpleTypes()
        {
            var actual = new OpenApiQueryResult <Dictionary <string, int> >
            {
                TotalCount  = 2,
                ResultItems = new[]
                {
                    new Dictionary <string, int>
                    {
                        ["A"] = 1,
                        ["b"] = 2,
                        ["C"] = 3
                    },
                    new Dictionary <string, int>
                    {
                        ["d"] = 4,
                        ["E"] = 5,
                        ["f"] = 6
                    }
                }
            };

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 2,
                ["value"]        = new object[]
                {
                    new Dictionary <string, int>
                    {
                        ["A"] = 1,
                        ["b"] = 2,
                        ["C"] = 3
                    },
                    new Dictionary <string, int>
                    {
                        ["d"] = 4,
                        ["E"] = 5,
                        ["f"] = 6
                    }
                }
            },
                            actual);
        }
コード例 #5
0
        public void TestSerialize_NativeArrays <T>(T[][] items)
        {
            var actual = new OpenApiQueryResult <ArrayWrapper <T> >
                             (4,
                             items.Select(i => new ArrayWrapper <T>
            {
                Items = i
            }).ToArray()
                             );

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 4,
                ["value"]        = items.Select(i => new
                {
                    items = i
                }).ToArray()
            },
                            actual);
        }
コード例 #6
0
        public override void Write(
            Utf8JsonWriter writer,
            OpenApiQueryResult <T> value,
            JsonSerializerOptions options)
        {
            writer.WriteStartObject();

            if (value.TotalCount != null)
            {
                writer.WriteNumber(ResultCountPropertyName, value.TotalCount.Value);
            }

            writer.WritePropertyName(ResultValuesPropertyName);

            var itemType     = _typeHandler.ResolveType(typeof(T));
            var selectClause = value.Options?.SelectExpand.RootSelectClause;

            JsonHelper.WriteArray(writer, value.ResultItems, itemType, selectClause, _typeHandler, options);

            writer.WriteEndObject();
        }
コード例 #7
0
        public void TestSerialize_PartialNavigationProperties_Collection()
        {
            var actual = new OpenApiQueryResult <CollectionNavigation>
            {
                Options = new OpenApiQueryOptions <CollectionNavigation>
                {
                    SelectExpand =
                    {
                        RootSelectClause          =
                        {
                            IsStarSelect  = false,
                            SelectClauses = new Dictionary <PropertyInfo, SelectClause>
                            {
                                [typeof(CollectionNavigation).GetProperty(nameof(CollectionNavigation.Nav1))] =
                                    new SelectClause
                                {
                                    IsStarSelect  = true
                                },
                                [typeof(CollectionNavigation).GetProperty(nameof(CollectionNavigation.Nav2))] =
                                    new SelectClause
                                {
                                    IsStarSelect  = false,
                                    SelectClauses = new Dictionary <PropertyInfo, SelectClause>
                                    {
                                        [typeof(SimpleClass).GetProperty(nameof(SimpleClass.StringProp))] =
                                            new SelectClause()
                                    }
                                }
                            }
                        }
                    }
                },
                TotalCount  = 2,
                ResultItems = new[]
                {
                    new CollectionNavigation
                    {
                        Nav1 = new[]
                        {
                            new SimpleClass
                            {
                                IntProp    = 1,
                                DoubleProp = 47.11,
                                StringProp = "A"
                            },
                            new SimpleClass
                            {
                                IntProp    = 2,
                                DoubleProp = 47.12,
                                StringProp = "B"
                            }
                        },
                        Nav2 = null,
                        Nav3 = new[]
                        {
                            new SimpleClass
                            {
                                IntProp    = 3,
                                DoubleProp = 47.13,
                                StringProp = "C"
                            }
                        }
                    },
                    new CollectionNavigation
                    {
                        Nav1 = new SimpleClass[0],
                        Nav2 = new[]
                        {
                            new SimpleClass
                            {
                                IntProp    = 4,
                                DoubleProp = 47.14,
                                StringProp = "D"
                            }
                        }
                    }
                }
            };

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 2,
                ["value"]        = new[]
                {
                    new
                    {
                        nav1 = new object[]
                        {
                            new
                            {
                                intProp    = 1,
                                doubleProp = 47.11,
                                stringProp = "A"
                            },
                            new
                            {
                                intProp    = 2,
                                doubleProp = 47.12,
                                stringProp = "B"
                            }
                        },
                        nav2 = (object)null
                    },
                    new
                    {
                        nav1 = new object[0],
                        nav2 = (object)new[]
                        {
                            new
                            {
                                stringProp = "D"
                            }
                        }
                    }
                }
            },
                            actual);
        }
コード例 #8
0
        public void TestSerialize_ObjectArrays_Polymorphic()
        {
            var actual = new OpenApiQueryResult <ArrayWrapper <Base> >
            {
                TotalCount  = 4,
                ResultItems = new[]
                {
                    new ArrayWrapper <Base>
                    {
                        Items = new Base[]
                        {
                            new Sub1
                            {
                                BaseProp = 1,
                                Sub1Prop = 47.11,
                                SubProp  = 47
                            },
                            new Sub2
                            {
                                BaseProp = 2,
                                Sub2Prop = "Test",
                                SubProp  = -47
                            }
                        }
                    },
                    new ArrayWrapper <Base>
                    {
                        Items = new Base[]
                        {
                            new Sub2
                            {
                                BaseProp = 3,
                                Sub2Prop = "A",
                                SubProp  = -11
                            },
                            new Sub1
                            {
                                BaseProp = 4,
                                Sub1Prop = 47.14,
                                SubProp  = 12
                            }
                        }
                    },
                }
            };

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 4,
                ["value"]        = new[]
                {
                    new
                    {
                        items = new object[]
                        {
                            new Dictionary <string, object>
                            {
                                ["@odata.type"] = "Sub1",
                                ["baseProp"]    = 1,
                                ["sub1Prop"]    = 47.11,
                                ["subProp"]     = 47
                            },
                            new Dictionary <string, object>
                            {
                                ["@odata.type"] = "Sub2",
                                ["baseProp"]    = 2,
                                ["sub2Prop"]    = "Test",
                                ["subProp"]     = -47
                            }
                        }
                    },
                    new
                    {
                        items = new object[]
                        {
                            new Dictionary <string, object>
                            {
                                ["@odata.type"] = "Sub2",
                                ["baseProp"]    = 3,
                                ["sub2Prop"]    = "A",
                                ["subProp"]     = -11
                            },
                            new Dictionary <string, object>
                            {
                                ["@odata.type"] = "Sub1",
                                ["baseProp"]    = 4,
                                ["sub1Prop"]    = 47.14,
                                ["subProp"]     = 12
                            }
                        }
                    }
                }
            },
                            actual);
        }
コード例 #9
0
        public void TestSerialize_ObjectArrays()
        {
            var actual = new OpenApiQueryResult <ArrayWrapper <SimpleClass> >
            {
                TotalCount  = 4,
                ResultItems = new[]
                {
                    new ArrayWrapper <SimpleClass>
                    {
                        Items = new[]
                        {
                            new SimpleClass
                            {
                                IntProp    = 1,
                                DoubleProp = 47.11,
                                StringProp = "A"
                            },
                            new SimpleClass
                            {
                                IntProp    = 2,
                                DoubleProp = 47.12,
                                StringProp = "B"
                            }
                        }
                    },
                    new ArrayWrapper <SimpleClass>
                    {
                        Items = new[]
                        {
                            new SimpleClass
                            {
                                IntProp    = 3,
                                DoubleProp = 47.13,
                                StringProp = "C"
                            },
                            new SimpleClass
                            {
                                IntProp    = 4,
                                DoubleProp = 47.14,
                                StringProp = "D"
                            }
                        }
                    }
                }
            };

            VerifySerialize(new Dictionary <string, object>
            {
                ["@odata.count"] = 4,
                ["value"]        = new[]
                {
                    new
                    {
                        items = new[]
                        {
                            new
                            {
                                intProp    = 1,
                                doubleProp = 47.11,
                                stringProp = "A"
                            },
                            new
                            {
                                intProp    = 2,
                                doubleProp = 47.12,
                                stringProp = "B"
                            }
                        }
                    },
                    new
                    {
                        items = new[]
                        {
                            new
                            {
                                intProp    = 3,
                                doubleProp = 47.13,
                                stringProp = "C"
                            },
                            new
                            {
                                intProp    = 4,
                                doubleProp = 47.14,
                                stringProp = "D"
                            }
                        }
                    }
                }
            },
                            actual);
        }
コード例 #10
0
        public override OpenApiQueryResult <T> Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException($"Unexpected JSON Token {reader.TokenType}.");
            }

            var actualClrType = typeof(T);

            var result = new OpenApiQueryResult <T>();

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(result);
                }

                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException($"Unexpected JSON Token {reader.TokenType}.");
                }

                switch (reader.GetString())
                {
                case ResultCountPropertyName:
                    if (!reader.Read())
                    {
                        throw new JsonException("Unexpected end of stream.");
                    }

                    switch (reader.TokenType)
                    {
                    case JsonTokenType.Number:
                        result.TotalCount = reader.GetInt64();
                        break;

                    case JsonTokenType.Null:
                        result.TotalCount = null;
                        break;

                    default:
                        throw new JsonException($"Unexpected JSON Token {reader.TokenType}.");
                    }

                    break;

                case ResultValuesPropertyName:
                    if (!reader.Read())
                    {
                        throw new JsonException("Unexpected end of stream.");
                    }

                    var document = JsonDocument.ParseValue(ref reader);
                    result.ResultItems = (T[])JsonHelper.ReadValue(document.RootElement,
                                                                   null,
                                                                   actualClrType.MakeArrayType(),
                                                                   _typeHandler
                                                                   );

                    break;
                }
            }

            throw new JsonException("Unexpected end of stream.");
        }