Пример #1
0
        public static void CallbackBuilder_Build()
        {
            var cbb = new OasCallbackBuilder()
            {
                [OasExpression.Parse("http://test/{$statusCode}")]   = new OasReferable <OasPath>("#/test"),
                [OasExpression.Parse("http://test/1/{$statusCode}")] = new OasReferable <OasPath>("#/test/1"),
            };
            var cb  = cbb.Build();
            var rcb = new OasCallbackBuilder(cb);

            Assert.Equal(cbb, cb);
            Assert.Equal(cbb, rcb);
        }
        public static void CompoundExpression_Parse()
        {
            var sut = OasExpression.Parse("http://example.com/api/foo?bar={$request.body#/bar}&baz={$url}&foo={$method}{$statusCode}");

            Assert.Equal(7, sut.Count);
            Assert.Equal(OasExpressionComponentType.Literal, sut[0].ComponentType);
            Assert.Equal(OasExpressionComponentType.Field, sut[1].ComponentType);
            Assert.Equal(OasExpressionComponentType.Literal, sut[2].ComponentType);
            Assert.Equal(OasExpressionComponentType.Field, sut[3].ComponentType);
            Assert.Equal(OasExpressionComponentType.Literal, sut[4].ComponentType);
            Assert.Equal(OasExpressionComponentType.Field, sut[5].ComponentType);
            Assert.Equal(OasExpressionComponentType.Field, sut[6].ComponentType);

            Assert.Equal("http://example.com/api/foo?bar=", sut[0].ToString());
            Assert.Equal("$request.body#/bar", sut[1].ToString());
            Assert.Equal("&baz=", sut[2].ToString());
            Assert.Equal("$url", sut[3].ToString());
            Assert.Equal("&foo=", sut[4].ToString());
            Assert.Equal("$method", sut[5].ToString());
            Assert.Equal("$statusCode", sut[6].ToString());
        }
Пример #3
0
        //[Fact(DisplayName = nameof(OpenApiSerializer_Serialize))]
        public static void OpenApiSerializer_Serialize()
        {
            var sut = new MockOasSerializer();

            #region Graph

            var actualGraphBuilder = new OasDocumentBuilder()
            {
                Components = new OasComponentsBuilder()
                {
                    Callbacks =
                    {
                        ["callback1"] = new OasCallbackBuilder()
                                    {
                                    [OasExpression.Parse("http://foo{$statusCode}")] = new OasPathBuilder()
                                    {
                                    Description = "path1",
                                    Delete      = new MockOasOperationBuilder()
                                    {
                                    OperationId = 1,
                                    Callbacks   =
                                    {
                                    ["callback1"] = "#/components/callbacks/callback1"
                                    },
                                    Description           = "Delete operation",
                                    ExternalDocumentation = new OasExternalDocumentationBuilder()
                                    {
                                    Description = "External docs 1",
                                    Url         = new Uri("http://example.org")
                                    },
                                    OperationIdentifier = "Operation 1",
                                    Options             = OasOperationOptions.Deprecated,
                                    Parameters          =
                                    {
                                    [new OasParameterKey("p1")] = "#/components/parameters/parameter1"
                                    },
                                    RequestBody = "#/components/requestBodies/requestBody1",
                                    Responses   =
                                    {
                                    [OasResponseKey.Default] = "#/components/responses/response1"
                                    },
                                    Summary = "Delete operation summary"
                                    },
                                    Get = new OasOperationBuilder()
                                    {
                                    Description = "Get"
                                    },
                                    Head = new OasOperationBuilder()
                                    {
                                    Description = "Head"
                                    },
                                    Options = new OasOperationBuilder()
                                    {
                                    Description = "Options"
                                    },
                                    Parameters =
                                    {
                                    [new OasParameterKey("p1")] = "#/components/parameters/parameter1"
                                    },
                                    Patch = new OasOperationBuilder()
                                    {
                                    Description = "Patch"
                                    },
                                    Post = new OasOperationBuilder()
                                    {
                                    Description = "Post"
                                    },
                                    Put = new OasOperationBuilder()
                                    {
                                    Description = "Put"
                                    },
                                    Summary = "Summary",
                                    Trace   = new OasOperationBuilder()
                                    {
                                    Description = "Trace"
                                    }
                                    }
                                    }
                    },
                    Examples =
                    {
                        ["example1"] = new OasExampleBuilder()
                        {
                        Description   = "Description",
                        ExternalValue = new Uri("http://example.org/example"),
                        Summary       = "Summary"
                        }
                    },
                    Headers =
                    {
                        ["header1"] = new OasParameterBodyBuilder()
                                            {
                                            Content =
                                            {
                                            [new ContentType("application/json")] = new OasMediaTypeBuilder()
                                            {
                                            Encoding =
                                            {
                                            ["header1"] = new OasPropertyEncodingBuilder()
                                            {
                                            ContentType = new ContentType("application/json"),
                                            Headers     =
                                            {
                                            ["header1"] = "#/components/headers/header1"
                                            },
                                            Options = OasPropertyEncodingOptions.AllowReserved | OasPropertyEncodingOptions.Explode,
                                            Style   = OasParameterStyle.DeepObject
                                            }
                                            },
                                            Examples =
                                            {
                                            ["Main Example"] = "#/components/examples/example1"
                                            },
                                            Schema = "#/components/schemas/schema1"
                                            }
                                            }
                                            }
                    },
                    Links =
                    {
                        ["link1"] = new OasLinkBuilder()
                                        {
                                        Description         = "Description",
                                        OperationIdentifier = "operation1",
                                        OperationReference  = "http://example.org/#/operation1",
                                        Server = new OasServerBuilder()
                                        {
                                        Description = "Description",
                                        Url         = new Uri("http://example.org"),
                                        Variables   =
                                        {
                                        ["variable1"] = new OasServerVariableBuilder()
                                        {
                                        Default     = "Value",
                                        Description = "Description",
                                        Enum        =
                                        {
                                        "Value", "Value1"
                                        }
                                        }
                                        }
                                        }
                                        }
                    },
                    Parameters =
                    {
                        ["parameter1"] = new OasParameterBuilder()
                                {
                                Content =
                                {
                                [new ContentType("application/json")] = new OasMediaTypeBuilder()
                                {
                                Schema = "#/components/schemas/schema1"
                                }
                                },
                                Description = "Description",
                                Examples    =
                                {
                                [new ContentType("application/json")] = "#/components/examples/example1"
                                },
                                Location = OasParameterLocation.Header,
                                Name     = "parameter1",
                                Options  = OasParameterOptions.AllowEmptyValue | OasParameterOptions.AllowReserved | OasParameterOptions.Deprecated | OasParameterOptions.Explode | OasParameterOptions.Required,
                                Schema   = "#/components/schemas/schema1",
                                Style    = OasParameterStyle.Matrix
                                }
                    },
                    RequestBodies =
                    {
                        ["requestbody1"] = new OasRequestBodyBuilder()
                                {
                                Content =
                                {
                                [new ContentType("application/json")] = new OasMediaTypeBuilder()
                                {
                                Schema = "#/components/schemas/schema1"
                                }
                                },
                                Description = "Description",
                                Options     = OasRequestBodyOptions.Required
                                }
                    },
                    Responses =
                    {
                        ["response1"] = new OasResponseBuilder()
                                {
                                Content =
                                {
                                [new ContentType("application/json")] = new OasMediaTypeBuilder()
                                {
                                Schema = "#/components/schemas/schema1"
                                }
                                },
                                Description = "Description",
                                Headers     =
                                {
                                ["header1"] = "#/components/headers/header1"
                                },
                                Links =
                                {
                                ["link1"] = "#/components/links/link1"
                                }
                                }
                    },
                    Schemas =
                    {
                        ["schema1"] = new OasSchemaBuilder()
                            {
                            AdditionalProperties =
                            {
                            ["prop1"] = "#/components/schemas/schema1"
                            },
                            Description           = "Description",
                            ExternalDocumentation = new OasExternalDocumentationBuilder()
                            {
                            Description = "Description",
                            Url         = new Uri("http://example.org/docs")
                            },
                            Format      = "Format",
                            Items       = "#/components/schemas/schema1",
                            ItemsRange  = new CountConstraint(100, 200),
                            LengthRange = new CountConstraint(200, 300),
                            NumberRange = new NumberConstraint(300, 400, RangeOptions.Exclusive),
                            Options     = OasSchemaOptions.Deprecated | OasSchemaOptions.Nullable | OasSchemaOptions.Required | OasSchemaOptions.UniqueItems,
                            Pattern     = "[a-z]",
                            Properties  =
                            {
                            ["prop1"] = "#/components/schemas/schema1"
                            },
                            PropertiesRange = new CountConstraint(100, 200),
                            Title           = "Schema1",
                            JsonType        = OasSchemaType.Object
                            }
                    },
                    SecuritySchemes =
                    {
                        ["sec1"] = new OasHttpSecuritySchemeBuilder()
                                {
                                BearerFormat = "Bearer",
                                Description  = "Description",
                                Scheme       = "Schema"
                                },
                        ["sec2"] = new OasApiKeySecuritySchemeBuilder()
                                {
                                Description = "Description",
                                Location    = OasParameterLocation.Cookie,
                                Name        = "Name"
                                },
                        ["sec3"] = new OasOidcSecuritySchemeBuilder()
                                {
                                Description = "Description",
                                Url         = new Uri("http://example.org/openid")
                                },
                        ["sec4"] = new OasOAuth2SecuritySchemeBuilder()
                                {
                                AuthorizationCodeFlow = new OasOAuthFlowBuilder()
                                {
                                AuthorizationUrl = new Uri("http://example.org/auth/auth"),
                                RefreshUrl       = new Uri("http://example.org/auth/refresh"),
                                TokenUrl         = new Uri("http://example.org/auth/token"),
                                Scopes           =
                                {
                                ["user:details"] = "Get the user details"
                                }
                                },
                                ClientCredentialsFlow = new OasOAuthFlowBuilder()
                                {
                                AuthorizationUrl = new Uri("http://example.org/cli/auth")
                                },
                                Description  = "Description",
                                ImplicitFlow = new OasOAuthFlowBuilder()
                                {
                                AuthorizationUrl = new Uri("http://example.org/imp/auth")
                                },
                                PasswordFlow = new OasOAuthFlowBuilder()
                                {
                                AuthorizationUrl = new Uri("http://example.org/pwd/auth")
                                }
                                }
                    }
                },
                ExternalDocumentation = new OasExternalDocumentationBuilder()
                {
                    Description = "Description",
                    Url         = new Uri("http://example.org/docs")
                },
                Info = new OasInformationBuilder()
                {
                    Contact = new OasContactBuilder()
                    {
                        Email = new MailAddress("*****@*****.**"),
                        Name  = "Jonathan",
                        Url   = new Uri("http://example.org/jonathan")
                    },
                    Description = "Description",
                    License     = new OasLicenseBuilder()
                    {
                        Name = "MIT",
                        Url  = new Uri("https://opensource.org/licenses/MIT")
                    },
                    TermsOfService = new Uri("http://example.org/tos"),
                    Title          = "Title",
                    Version        = new SemanticVersion(1, 2, 3, "pre1", "build1")
                },
                Version = new SemanticVersion(3, 0, 1),
                Paths   =
                {
                    ["path1"] = new OasPathBuilder()
                    {
                    Description = "Description"
                    }
                },
                Security =
                {
                    "#/components/security/sec1"
                }
            };

            var actualJson = sut.Serialize(actualGraphBuilder.Build());

            #endregion

            #region Json

            var expectedJson = new JObject()
            {
                ["components"] = new JObject()
                {
                    ["callbacks"] = new JObject()
                    {
                        ["callback1"] = new JObject()
                        {
                            ["http://foo{$statusCode}"] = new JObject()
                            {
                                ["delete"] = new JObject()
                                {
                                    ["callbacks"] = new JObject()
                                    {
                                        ["callback1"] = new JObject()
                                        {
                                            ["$ref"] = "#/components/callbacks/callback1"
                                        }
                                    },
                                    ["deprecated"]   = true,
                                    ["description"]  = "Delete operation",
                                    ["externalDocs"] = new JObject()
                                    {
                                        ["description"] = "External docs 1",
                                        ["url"]         = "http://example.org/"
                                    },
                                    ["operationId"] = "Operation 1",
                                    ["parameters"]  = new JArray()
                                    {
                                        new JObject()
                                        {
                                            ["$ref"] = "#/components/parameters/parameter1"
                                        }
                                    },
                                    ["requestBody"] = new JObject()
                                    {
                                        ["$ref"] = "#/components/requestBodies/requestBody1"
                                    },
                                    ["responses"] = new JObject()
                                    {
                                        ["default"] = new JObject()
                                        {
                                            ["$ref"] = "#/components/responses/response1"
                                        }
                                    },
                                    ["summary"]           = "Delete operation summary",
                                    ["x-k2-operation-id"] = "0000000000000001"
                                },
                                ["description"] = "path1",
                                ["get"]         = new JObject()
                                {
                                    ["description"] = "Get"
                                },
                                ["head"] = new JObject()
                                {
                                    ["description"] = "Head"
                                },
                                ["options"] = new JObject()
                                {
                                    ["description"] = "Options"
                                },
                                ["parameters"] = new JArray()
                                {
                                    new JObject()
                                    {
                                        ["$ref"] = "#/components/parameters/parameter1"
                                    }
                                },
                                ["patch"] = new JObject()
                                {
                                    ["description"] = "Patch"
                                },
                                ["post"] = new JObject()
                                {
                                    ["description"] = "Post"
                                },
                                ["put"] = new JObject()
                                {
                                    ["description"] = "Put"
                                },
                                ["summary"] = "Summary",
                                ["trace"]   = new JObject()
                                {
                                    ["description"] = "Trace"
                                }
                            }
                        }
                    },
                    ["examples"] = new JObject()
                    {
                        ["example1"] = new JObject()
                        {
                            ["description"]   = "Description",
                            ["externalValue"] = "http://example.org/example",
                            ["summary"]       = "Summary"
                        }
                    },
                    ["headers"] = new JObject()
                    {
                        ["header1"] = new JObject()
                        {
                            ["content"] = new JObject()
                            {
                                ["application/json"] = new JObject()
                                {
                                    ["encoding"] = new JObject()
                                    {
                                        ["header1"] = new JObject()
                                        {
                                            ["allowReserved"] = true,
                                            ["contentType"]   = "application/json",
                                            ["explode"]       = true,
                                            ["headers"]       = new JObject()
                                            {
                                                ["header1"] = new JObject()
                                                {
                                                    ["$ref"] = "#/components/headers/header1"
                                                }
                                            },
                                            ["style"] = "deepObject"
                                        }
                                    },
                                    ["examples"] = new JObject()
                                    {
                                        ["Main Example"] = new JObject()
                                        {
                                            ["$ref"] = "#/components/examples/example1"
                                        }
                                    },
                                    ["schema"] = new JObject()
                                    {
                                        ["$ref"] = "#/components/schemas/schema1"
                                    }
                                }
                            }
                        }
                    },
                    ["links"] = new JObject()
                    {
                        ["link1"] = new JObject()
                        {
                            ["description"]  = "Description",
                            ["operationId"]  = "operation1",
                            ["operationRef"] = "http://example.org/#/operation1",
                            ["server"]       = new JObject()
                            {
                                ["description"] = "Description",
                                ["url"]         = "http://example.org/",
                                ["variables"]   = new JObject()
                                {
                                    ["variable1"] = new JObject()
                                    {
                                        ["default"]     = "Value",
                                        ["description"] = "Description",
                                        ["enum"]        = new JArray()
                                        {
                                            "Value",
                                            "Value1"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    ["parameters"] = new JObject()
                    {
                        ["parameter1"] = new JObject()
                        {
                            ["allowEmptyValue"] = true,
                            ["allowReserved"]   = true,
                            ["content"]         = new JObject()
                            {
                                ["application/json"] = new JObject()
                                {
                                    ["schema"] = new JObject()
                                    {
                                        ["$ref"] = "#/components/schemas/schema1"
                                    }
                                }
                            },
                            ["deprecated"]  = true,
                            ["description"] = "Description",
                            ["examples"]    = new JObject()
                            {
                                ["application/json"] = new JObject()
                                {
                                    ["$ref"] = "#/components/examples/example1"
                                }
                            },
                            ["explode"]  = true,
                            ["in"]       = "header",
                            ["name"]     = "parameter1",
                            ["required"] = true,
                            ["schema"]   = new JObject()
                            {
                                ["$ref"] = "#/components/schemas/schema1"
                            },
                            ["style"] = "matrix"
                        }
                    },
                    ["requestBodies"] = new JObject()
                    {
                        ["requestbody1"] = new JObject()
                        {
                            ["content"] = new JObject()
                            {
                                ["application/json"] = new JObject()
                                {
                                    ["schema"] = new JObject()
                                    {
                                        ["$ref"] = "#/components/schemas/schema1"
                                    }
                                }
                            },
                            ["description"] = "Description",
                            ["required"]    = true
                        }
                    },
                    ["responses"] = new JObject()
                    {
                        ["response1"] = new JObject()
                        {
                            ["content"] = new JObject()
                            {
                                ["application/json"] = new JObject()
                                {
                                    ["schema"] = new JObject()
                                    {
                                        ["$ref"] = "#/components/schemas/schema1"
                                    }
                                }
                            },
                            ["description"] = "Description",
                            ["headers"]     = new JObject()
                            {
                                ["header1"] = new JObject()
                                {
                                    ["$ref"] = "#/components/headers/header1"
                                }
                            },
                            ["links"] = new JObject()
                            {
                                ["link1"] = new JObject()
                                {
                                    ["$ref"] = "#/components/links/link1"
                                }
                            }
                        }
                    },
                    ["schemas"] = new JObject()
                    {
                        ["schema1"] = new JObject()
                        {
                            ["additionalProperties"] = new JObject()
                            {
                                ["prop1"] = new JObject()
                                {
                                    ["$ref"] = "#/components/schemas/schema1"
                                }
                            },
                            ["deprecated"]       = true,
                            ["description"]      = "Description",
                            ["exclusiveMaximum"] = true,
                            ["exclusiveMinimum"] = true,
                            ["externalDocs"]     = new JObject()
                            {
                                ["description"] = "Description",
                                ["url"]         = "http://example.org/docs"
                            },
                            ["format"] = "Format",
                            ["items"]  = new JObject()
                            {
                                ["$ref"] = "#/components/schemas/schema1"
                            },
                            ["maxLength"]     = 300,
                            ["maxProperties"] = 200,
                            ["maximum"]       = 400,
                            ["minLength"]     = 200,
                            ["minProperties"] = 100,
                            ["minimum"]       = 300,
                            ["nullable"]      = true,
                            ["pattern"]       = "[a-z]",
                            ["properties"]    = new JObject()
                            {
                                ["prop1"] = new JObject()
                                {
                                    ["$ref"] = "#/components/schemas/schema1"
                                }
                            },
                            ["required"]    = true,
                            ["title"]       = "Schema1",
                            ["type"]        = "object",
                            ["uniqueItems"] = true
                        }
                    },
                    ["securitySchemes"] = new JObject()
                    {
                        ["sec1"] = new JObject()
                        {
                            ["bearerFormat"] = "Bearer",
                            ["description"]  = "Description",
                            ["scheme"]       = "Schema",
                            ["type"]         = "http"
                        },
                        ["sec2"] = new JObject()
                        {
                            ["description"] = "Description",
                            ["in"]          = "cookie",
                            ["name"]        = "Name",
                            ["type"]        = "apiKey"
                        },
                        ["sec3"] = new JObject()
                        {
                            ["description"]      = "Description",
                            ["openIdConnectUrl"] = "http://example.org/openid",
                            ["type"]             = "openIdConnect"
                        },
                        ["sec4"] = new JObject()
                        {
                            ["description"] = "Description",
                            ["flows"]       = new JObject()
                            {
                                ["authorizationCode"] = new JObject()
                                {
                                    ["authorizationUrl"] = "http://example.org/auth/auth",
                                    ["refreshUrl"]       = "http://example.org/auth/refresh",
                                    ["scopes"]           = new JObject()
                                    {
                                        ["user:details"] = "Get the user details"
                                    },
                                    ["tokenUrl"] = "http://example.org/auth/token"
                                },
                                ["clientCredentials"] = new JObject()
                                {
                                    ["authorizationUrl"] = "http://example.org/cli/auth",
                                    ["tokenUrl"]         = null
                                },
                                ["implicit"] = new JObject()
                                {
                                    ["authorizationUrl"] = "http://example.org/imp/auth",
                                    ["tokenUrl"]         = null
                                },
                                ["password"] = new JObject()
                                {
                                    ["authorizationUrl"] = "http://example.org/pwd/auth",
                                    ["tokenUrl"]         = null
                                }
                            },
                            ["type"] = "oauth2"
                        }
                    }
                },
                ["externalDocs"] = new JObject()
                {
                    ["description"] = "Description",
                    ["url"]         = "http://example.org/docs"
                },
                ["info"] = new JObject()
                {
                    ["contact"] = new JObject()
                    {
                        ["email"] = "*****@*****.**",
                        ["name"]  = "Jonathan",
                        ["url"]   = "http://example.org/jonathan"
                    },
                    ["description"] = "Description",
                    ["license"]     = new JObject()
                    {
                        ["name"] = "MIT",
                        ["url"]  = "https://opensource.org/licenses/MIT"
                    },
                    ["termsOfService"] = "http://example.org/tos",
                    ["title"]          = "Title",
                    ["version"]        = "1.2.3-pre1+build1"
                },
                ["openapi"] = "3.0.1",
                ["paths"]   = new JObject()
                {
                    ["path1"] = new JObject()
                    {
                        ["description"] = "Description"
                    }
                },
                ["security"] = new JArray()
                {
                    new JObject()
                    {
                        ["$ref"] = "#/components/security/sec1"
                    }
                }
            };

            #endregion

            var ej = expectedJson.ToString();
            var aj = actualJson.ToString();

            // TODO: This fails
            //Assert.Equal(expectedJson, actualJson, JTokenComparer.Default);
        }
Пример #4
0
 /// <summary>Gets the element that has the specified key in the read-only dictionary.</summary>
 /// <param name="key">The key to locate.</param>
 /// <returns>The element that has the specified key in the read-only dictionary.</returns>
 /// <exception cref="T:System.ArgumentNullException">
 ///   <paramref name="key">key</paramref> is null.</exception>
 /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name="key">key</paramref> is not found.</exception>
 public OasReferable <OasPath> this[OasExpression key]
 {
     get => _dictionary[key];
Пример #5
0
 /// <summary>Gets the value that is associated with the specified key.</summary>
 /// <param name="key">The key to locate.</param>
 /// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
 /// <returns>true if the object that implements the <see cref="T:System.Collections.Generic.IReadOnlyDictionary`2"></see> interface contains an element that has the specified key; otherwise, false.</returns>
 /// <exception cref="T:System.ArgumentNullException">
 ///   <paramref name="key">key</paramref> is null.</exception>
 public bool TryGetValue(OasExpression key, out OasReferable <OasPath> value) => _dictionary.TryGetValue(key, out value);
Пример #6
0
 /// <summary>Determines whether the read-only dictionary contains an element that has the specified key.</summary>
 /// <param name="key">The key to locate.</param>
 /// <returns>true if the read-only dictionary contains an element that has the specified key; otherwise, false.</returns>
 /// <exception cref="T:System.ArgumentNullException">
 ///   <paramref name="key">key</paramref> is null.</exception>
 public bool ContainsKey(OasExpression key) => _dictionary.ContainsKey(key);