コード例 #1
0
 private RuleBase(OpenApiDocument contract, Supressions supressions, IOptions <RuleSettings> ruleSettings, OpenApiDocumentCache cache)
 {
     this.RuleSettings = ruleSettings;
     this.Contract     = contract;
     this.Supressions  = supressions;
     this.Cache        = cache;
 }
コード例 #2
0
        public VersionFormatRuleTest()
        {
            const string contract = "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"1.2.3\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>());

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                VersionFormat = new VersionFormatConfig()
                {
                    RegexExpectedFormat  = "^(v\\d+)$",
                    HumanReadeableFormat = "'v' + integer version number",
                    Example = "v2"
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #3
0
        public PathWithCrudNamesRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/gravar-path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/apagar-path-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/gravar-path-one'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                PathWithCrudNames = new PathWithCrudNamesConfig()
                {
                    WordsToAvoid = "get,consultar,recuperar,listar,ler,obter,post,salvar,gravar,enviar,postar,path,atualizar,delete,apagar,deletar,remover,excluir"
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #4
0
        public DateWithoutFormatRuleTest()
        {
            const string contract = "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"title\": \"teste 2\",\n    \"version\": \"1.0\"\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                 \"zero\" : {\n                  \"type\" : \"string\",\n                  \"example\": \"2020-07-09\"\n                },\n                \"one\" : {\n                  \"type\" : \"string\",\n                  \"example\": \"2020-07-08\"\n                },\n                \"two\" : {\n                  \"type\" : \"string\",\n                  \"example\": \"2017-07-21T17:32:28Z\"\n                },\n                \"three\" : {\n                  \"type\" : \"string\",\n                  \"example\": \"2019/09/11\"\n                },\n                \"four\" : {\n                  \"type\" : \"string\",\n                  \"example\": \"13:29\"\n                },\n                \"five\" : {\n                  \"type\" : \"string\",\n                  \"example\": \"04/03/1998\"\n                },\n                 \"six\" : {\n                  \"type\" : \"string\",\n                  \"example\": \"2017-07-21\",\n                  \"format\": \"date\"\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"seven\" : {\n                      \"type\" : \"string\",\n                      \"example\": \"6\"\n                    },\n                    \"eight\" : {\n                      \"type\" : \"string\",\n                      \"example\": \"A77\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-one',Operation='get',ResponseCode='200',Parameter='zero'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 5
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #5
0
        public HealthCheckRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/gravar-path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/apagar-path-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>());

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                HealthCheck = new HealthCheckConfig()
                {
                    Regex = "^(health)$"
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #6
0
 /// <summary>
 /// Validates if parameters or attributes have examples indicating that the content is likely numeric type.
 /// Supressions: Rule,Path,Operation,ResponseCode,Content,PropertyFull
 /// </summary>
 public StringCouldBeNumberRule(OpenApiDocument contract,
                                Supressions supressions,
                                IOptions <RuleSettings> ruleSettings, OpenApiDocumentCache cache)
     : base(contract, supressions, ruleSettings, cache, ruleName, Severity.Hint)
 {
     exceptionsRegex = ruleSettings.Value.StringCouldBeNumber.ExceptionsRegex;
 }
コード例 #7
0
        public ContentEnvelopeRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"noDataEnvelope\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"noDataEnvelope\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-three\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-four\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Status 200\"\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            var settings = new RuleSettings()
            {
                ContentEnvelope = new ContentEnvelopeConfig()
                {
                    EnvelopeName = "data"
                }
            };

            ruleSettings = Options.Create(settings);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/path-one',Operation='get',ResponseCode='200'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            cache = new OpenApiDocumentCache();
        }
コード例 #8
0
        public DescriptionQualityRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"ok\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"parameters\" : [{\n            \"in\" : \"query\",\n            \"name\" : \"parameterOne\",\n            \"type\" : \"string\",\n            \"description\" : \"Too short, but ok.\",\n            \"required\" : true\n        }]\n        ,\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"Ok\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\",\n                      \"description\" : \"bad description\"\n                    },\n                    \"propertyTwo\" : {\n                      \"type\" : \"string\",\n                      \"description\" : \"another bad description\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-two',Operation='get',ResponseCode='200',PropertyFull='data.propertyOne'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            cache = new OpenApiDocumentCache();
        }
コード例 #9
0
        public EmptyExamplesRuleTest()
        {
            const string contract =
                "{\r\n  \"swagger\": \"2.0\",\r\n  \"info\": {\r\n    \"title\": \"teste 2\",\r\n    \"version\": \"1.0\"\r\n  },\r\n  \"consumes\": [\r\n    \"application/json\"\r\n  ],\r\n  \"produces\": [\r\n    \"application/json\"\r\n  ],\r\n  \"paths\": {\r\n    \"/path-one\": {\r\n      \"get\": {\r\n        \"responses\": {\r\n          \"200\": {\r\n            \"description\": \"Status 200\",\r\n            \"schema\": {\r\n              \"type\": \"object\",\r\n              \"properties\": {\r\n                \"data\": {\r\n                  \"type\": \"object\",\r\n                  \"properties\": {\r\n                    \"propertyOne\": {\r\n                      \"type\": \"string\",\r\n                      \"example\": \"An example\"\r\n                    },\r\n                    \"propertyTwo\": {\r\n                      \"type\": \"string\"\r\n                    }\r\n                  }\r\n                },\r\n                \"propertyThree\": {\r\n                  \"type\": \"string\"\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-two\": {\r\n      \"get\": {\r\n        \"responses\": {\r\n          \"200\": {\r\n            \"description\": \"Status 200\",\r\n            \"schema\": {\r\n              \"type\": \"object\",\r\n              \"properties\": {\r\n                \"data\": {\r\n                  \"type\": \"object\",\r\n                  \"properties\": {\r\n                    \"propertyFour\": {\r\n                      \"type\": \"string\"\r\n                    }\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-three\": {\r\n      \"get\": {\r\n        \"responses\": {\r\n          \"200\": {\r\n            \"description\": \"Status 200\",\r\n            \"schema\": {\r\n              \"type\": \"object\",\r\n              \"properties\": {\r\n                \"data\": {\r\n                  \"type\": \"object\",\r\n                  \"properties\": {\r\n                    \"propertyOne\": {\r\n                      \"type\": \"string\"\r\n                    },\r\n                    \"propertyTwo\": {\r\n                      \"type\": \"string\"\r\n                    }\r\n                  }\r\n                },\r\n                \"propertyThree\": {\r\n                  \"type\": \"string\"\r\n                }\r\n              }\r\n            },\r\n            \"examples\": {\r\n              \"example-1\": {\r\n                \"data\": {\r\n                  \"propertyOne\": \"An example at schema for propertyOne\",\r\n                  \"propertyTwo\": \"An example at schema for propertyTwo\"\r\n                },\r\n                \"propertyThree\": \"An example at schema for propertyThree\"\r\n              },\r\n              \"example-2\": {\r\n                \"data\": {\r\n                  \"propertyOne\": \"ex2\",\r\n                  \"propertyTwo\": \"ex2\"\r\n                },\r\n                \"propertyThree\": \"ex2\"\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-four\": {\r\n      \"get\": {\r\n        \"responses\": {\r\n          \"200\": {\r\n            \"description\": \"Status 200\",\r\n            \"schema\": {\r\n              \"type\": \"object\",\r\n              \"properties\": {\r\n                \"data\": {\r\n                  \"type\": \"array\",\r\n                  \"items\": {\r\n                    \"properties\": {\r\n                      \"propertyOne\": {\r\n                        \"type\": \"string\",\r\n                        \"example\": \"examplePropertyOne\"\r\n                      },\r\n                      \"propertyTwo\": {\r\n                        \"type\": \"string\",\r\n                        \"example\": \"examplePropertyTwo\"\r\n                      }\r\n                    }\r\n                  }\r\n                },\r\n                \"propertyThree\": {\r\n                  \"type\": \"string\",\r\n                  \"example\": \"examplePropertyThree\"\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-five\": {\r\n      \"get\": {\r\n        \"responses\": {\r\n          \"200\": {\r\n            \"description\": \"Status 200\",\r\n            \"schema\": {\r\n              \"type\": \"object\",\r\n              \"properties\": {\r\n                \"data\": {\r\n                  \"type\": \"array\",\r\n                  \"items\": {\r\n                  \"properties\": {\r\n                    \"propertyOne\": {\r\n                      \"type\": \"string\"\r\n                    },\r\n                    \"propertyTwo\": {\r\n                      \"type\": \"string\"\r\n                    }\r\n                  }\r\n                  }\r\n                },\r\n                \"propertyThree\": {\r\n                  \"type\": \"string\"\r\n                }\r\n              }\r\n            },\r\n            \"examples\": {\r\n              \"example-1\": {\r\n                \"data\": [{\r\n                  \"propertyOne\": \"An example at schema for propertyOne\",\r\n                  \"propertyTwo\": \"An example at schema for propertyTwo\"\r\n                }],\r\n                \"propertyThree\": \"An example at schema for propertyThree\"\r\n              },\r\n              \"example-2\": {\r\n                \"data\": {\r\n                  \"propertyOne\": \"ex2\",\r\n                  \"propertyTwo\": \"ex2\"\r\n                },\r\n                \"propertyThree\": \"ex2\"\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-two',Operation='get',ResponseCode='200'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 5
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #10
0
        public PathPluralRuleTest()
        {
            const string contract =
                "{\r\n  \"swagger\" : \"2.0\",\r\n  \"info\" : {\r\n    \"version\" : \"v1\",\r\n    \"title\" : \"Swagger Test\",\r\n  },\r\n  \"consumes\" : [ \"application/json\" ],\r\n  \"produces\" : [ \"application/json\" ],\r\n  \"paths\" : {\r\n    \"/path-one\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will not hit the Rule because there are not data envelope\",\r\n            \"schema\" : {\r\n              \"type\" : \"object\",\r\n              \"properties\" : {\r\n                \"noDataEnvelope\" : {\r\n                  \"type\" : \"object\",\r\n                  \"properties\" : {\r\n                    \"propertyOne\" : {\r\n                      \"type\" : \"string\"\r\n                    }\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/paths-two\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will hit the rule: plural, data, no array.\",\r\n            \"schema\" : {\r\n              \"type\" : \"object\",\r\n              \"properties\" : {\r\n                \"data\" : {\r\n                  \"type\" : \"object\",\r\n                  \"properties\" : {\r\n                    \"propertyOne\" : {\r\n                      \"type\" : \"string\"\r\n                    }\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-three\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will not hit the rule: singular\",\r\n            \"schema\" : {\r\n              \"type\" : \"object\",\r\n              \"properties\" : {\r\n                \"data\" : {\r\n                  \"type\" : \"object\",\r\n                  \"properties\" : {\r\n                    \"propertyOne\" : {\r\n                      \"type\" : \"string\"\r\n                    }\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-fours\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will not hit the rule: empty\"\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-fives\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will not hit the rule: array ok\",\r\n            \"schema\" : {\r\n              \"type\" : \"object\",\r\n              \"properties\" : {\r\n                \"data\" : {\r\n                  \"type\" : \"array\",\r\n                  \"items\": {\r\n           \"type\" : \"object\",\r\n                    \"properties\" : {\r\n                      \"propertyOne\" : {\r\n                      \"type\" : \"string\"\r\n                      }\r\n                    }\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/paths-six/{PathSixId}\" : {\r\n      \"get\" : {\r\n        \"parameters\": [ {\r\n          \"in\": \"path\",\r\n          \"name\": \"PathSixId\",\r\n          \"required\": true,\r\n          \"type\": \"string\"\r\n        }],\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will not hit the rule: {id}\"\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/paths-seven/{PathSixId}\" : {\r\n      \"get\" : {\r\n        \"parameters\": [ {\r\n          \"in\": \"path\",\r\n          \"name\": \"PathSixId\",\r\n          \"required\": true,\r\n          \"type\": \"string\"\r\n        }],\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will not hit the rule: {id}\"\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/health\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Will not hit the rule, because it is at exceptions.\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/path-one'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                PathPlural = new PathPluralConfig()
                {
                    Exceptions = "health,xpto"
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();

            ruleSettings.Value.PathPlural.Exceptions = "health,xpto";
        }
コード例 #11
0
        public ContentIn204RuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"204\" : {\n            \"description\" : \"Status 204\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"204\" : {\n            \"description\" : \"Status 204\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-three\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"204\" : {\n            \"description\" : \"Status 204\"\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(
                new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName, Target = "Path='/path-one',Operation='get'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            cache = new OpenApiDocumentCache();
        }
コード例 #12
0
        public ValidResponseCodesRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"299\" : {\n            \"description\" : \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"parameters\" : [{\n            \"in\" : \"query\",\n            \"name\" : \"parameterOne\",\n            \"type\" : \"string\",\n            \"description\" : \"too short\",\n            \"required\" : true\n        }]\n        ,\n        \"responses\" : {\n          \"418\" : {\n            \"description\": \"Status 418\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/path-one',Operation='get',ResponseCode='299'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                ValidResponseCodes = new ValidResponseCodesConfig()
                {
                    ValidHttpCodes = "200,201,202,204,206,301,303,304,307,400,401,403,404,405,410,414,422,428,429,500,501,503,504"
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #13
0
        public PathCaseRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/pathOne\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/pathTwo\" : {\n      \"get\" : {\n        \"parameters\" : [{\n            \"in\" : \"query\",\n            \"name\" : \"parameterOne\",\n            \"type\" : \"string\",\n            \"description\" : \"too short\",\n            \"required\" : true\n        }]\n        ,\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/pathOne'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                PathCase = new PathCaseConfig()
                {
                    CaseType = "KebabCase",
                    CaseTypeTolerateNumber = true,
                    Example = "distritos-federais"
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #14
0
        public ArrayOnNoResourceIdEndpointTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"It will not hit the Rule because there are no data envelope\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"noDataEnvelope\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/paths-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"It will hit the rule: plural, data, no array.\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-three\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"It will not hit the rule: singular\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-fours\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"It will not hit the rule: empty\"\n          }\n        }\n      }\n    },\n    \"/path-fives\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"It will not hit the rule: array ok\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"array\",\n                  \"items\": {\n \t\t\t\t\t\"type\" : \"object\",\n                    \"properties\" : {\n                      \"propertyOne\" : {\n                    \t\"type\" : \"string\"\n                      }\n                  \t}\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/paths-six/{PathSixId}\" : {\n      \"get\" : {\n        \"parameters\": [ {\n          \"in\": \"path\",\n          \"name\": \"PathSixId\",\n          \"required\": true,\n          \"type\": \"string\"\n        }],\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"It will not hit the rule: {id}\"\n          }\n        }\n      }\n    },\n    \"/paths-seven\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"It will hit the rule: plural, data, no array.\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            var settings = new RuleSettings()
            {
                ArrayOnNoResourceIdEndpoint = new ArrayOnNoResourceIdEndpointConfig()
                {
                    Example = "\"data\": [ { object 1 }, { object n } ]"
                }
            };

            ruleSettings = Options.Create(settings);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/paths-two',Operation='get',ResponseCode='200'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            cache = new OpenApiDocumentCache();
        }
コード例 #15
0
        public PropertyNamingMatchingPathRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"addressPathOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"addressPathTwo\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-one',Operation='get',ResponseCode='200',PropertyFull='data.addressPathOne'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 20
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #16
0
 /// <summary>
 /// Validate the version format.
 /// Supressions: Rule
 /// </summary>
 public VersionFormatRule(OpenApiDocument contract, Supressions supressions,
                          IOptions <RuleSettings> ruleSettings, OpenApiDocumentCache cache)
     : base(contract, supressions, ruleSettings, cache, ruleName, Severity.Error)
 {
     regexExpectedFormat  = ruleSettings.Value.VersionFormat.RegexExpectedFormat;
     humanReadeableFormat = ruleSettings.Value.VersionFormat.HumanReadeableFormat;
     example = ruleSettings.Value.VersionFormat.Example;
 }
コード例 #17
0
        /// <summary>
        /// Validate if a likelihood plural collection path (without {id}) have an array as response.
        /// Supressions: Rule,Path
        /// </summary>
        public ArrayOnNoResourceIdEndpointRule(OpenApiDocument contract, Supressions supressions,
                                               IOptions <RuleSettings> ruleSettings, OpenApiDocumentCache cache) :
            base(contract, supressions, ruleSettings, cache, ruleName, Severity.Warning)
        {
            var example =
                ruleSettings.Value.ArrayOnNoResourceIdEndpoint.Example;

            Description = Details.Replace("{0}", example);
        }
コード例 #18
0
 protected RuleBase(OpenApiDocument contract, Supressions supressions,
                    IOptions <RuleSettings> ruleSettings, OpenApiDocumentCache cache, string ruleName, Severity severity) :
     this(contract, supressions, ruleSettings, cache)
 {
     Name          = TryGetValueFromResource($"{ruleName}.Name");
     Description   = TryGetValueFromResource($"{ruleName}.Description");
     Details       = TryGetValueFromResource($"{ruleName}.Details");
     Severity      = severity;
     this.ruleName = ruleName;
 }
コード例 #19
0
        public BaseUrlRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"host\" : \"/api.safra.com.br\",\n  \"basePath\" : \"/recursos-humanos\",\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"schemes\" : [ \"https\" ],\n  \"paths\" : {\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>());

            supressionEntireRule = new Supressions(new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            cache = new OpenApiDocumentCache();
        }
コード例 #20
0
        /// <summary>
        /// Validate the paths case type.
        /// Supressions: Rule,Path
        /// </summary>
        public PathCaseRule(OpenApiDocument contract, Supressions supressions,
                            IOptions <RuleSettings> ruleSettings, OpenApiDocumentCache cache) :
            base(contract, supressions, ruleSettings, cache, ruleName, Severity.Warning)
        {
            if (Enum.TryParse(ruleSettings.Value.PathCase.CaseType, out CaseType caseTypeConversionTryOut))
            {
                caseType = caseTypeConversionTryOut;
            }
            else
            {
                throw new InvalidEnumArgumentException(
                          $"{ruleName}.CaseType:{ruleSettings.Value.PathCase.CaseType}");
            }

            caseTypeTolerateNumber = ruleSettings.Value.PathCase.CaseTypeTolerateNumber;
            example = ruleSettings.Value.PathCase.Example;
        }
コード例 #21
0
        public PathParameterRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one/{idNothingToMatch}\" : {\n      \"parameters\": [\n        {\n          \"type\": \"string\",\n          \"name\": \"idNothingToMatch\",\n          \"in\": \"path\",\n          \"required\": true\n        }\n      ],\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two/{idNothingToMatch}\" : {\n      \"get\" : {\n        \"parameters\": [\n          {\n            \"type\": \"string\",\n            \"name\": \"idNothingToMatch\",\n            \"in\": \"path\",\n            \"required\": true\n          }\n        ],\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-one/{idNothingToMatch}',Operation='get',Parameter='idNothingToMatch'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                PathParameter = new PathParameterConfig()
                {
                    CaseType                  = "CamelCase",
                    Example                   = "idCliente",
                    PrefixToRemove            = "id",
                    SufixToRemove             = "",
                    Regex                     = "^(id[a-zA-Z]+)$",
                    MatchEntityNamePercentage = 0.6,
                    HumanReadeableFormat      = "'id' + 'NomeSingularDoPath'"
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #22
0
        /// <summary>
        /// Validate the path parameters case type.
        /// Supressions: Rule,Path,Operation,Parameter
        /// </summary>
        public PathParameterRule(OpenApiDocument contract, Supressions supressions,
                                 IOptions <RuleSettings> ruleSettings, OpenApiDocumentCache cache)
            : base(contract, supressions, ruleSettings, cache, ruleName, Severity.Warning)
        {
            matchEntityNamePercentage = ruleSettings.Value.PathParameter.MatchEntityNamePercentage;
            regex                = ruleSettings.Value.PathParameter.Regex;
            prefixToRemove       = ruleSettings.Value.PathParameter.PrefixToRemove;
            sufixToRemove        = ruleSettings.Value.PathParameter.SufixToRemove;
            humanReadeableFormat = ruleSettings.Value.PathParameter.HumanReadeableFormat;
            example              = ruleSettings.Value.PathParameter.Example;

            if (Enum.TryParse(ruleSettings.Value.PathParameter.CaseType, out CaseType caseTypeConversionTryOut))
            {
                caseType = caseTypeConversionTryOut;
            }
            else
            {
                throw new InvalidEnumArgumentException(
                          $"{ruleName}.CaseType={ruleSettings.Value.PathParameter.CaseType}");
            }
        }
コード例 #23
0
        public PaginationEnvelopeFormatRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                },\n                \"pagination\"  : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"parameters\" : [{\n            \"in\" : \"query\",\n            \"name\" : \"parameterOne\",\n            \"type\" : \"string\",\n            \"description\" : \"too short\",\n            \"required\" : true\n        }]\n        ,\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                },\n                \"pagination\"  : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-one',Operation='get',ResponseCode='200'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                PaginationEnvelopeFormat = new PaginationEnvelopeFormatConfig()
                {
                    PropertiesInPagination         = "first,last,previous,next,page,isFirst,isLast,totalElements",
                    PaginationEnvelopePropertyName = "pagination"
                },
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 20
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #24
0
        public ErrorResponseFormatRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"400\" : {\n            \"description\": \"Status 400\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"codeX\" : {\n                  \"type\" : \"string\"\n                },\n                \"messageX\" : {\n                  \"type\" : \"string\"\n                },\n                \"detailsX\" : {\n                  \"type\" : \"string\"\n                }\n              }                \n            }\n          },\n          \"500\" : {\n            \"description\": \"Status 500\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"codeX\" : {\n                  \"type\" : \"string\"\n                },\n                \"messageX\" : {\n                  \"type\" : \"string\"\n                },\n                \"detailsX\" : {\n                  \"type\" : \"string\"\n                }\n              }                \n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"500\" : {\n            \"description\": \"Status 200\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"codeX\" : {\n                  \"type\" : \"string\"\n                },\n                \"messageX\" : {\n                  \"type\" : \"string\"\n                },\n                \"detailsX\" : {\n                  \"type\" : \"string\"\n                }\n              }                \n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-one',Operation='get',ResponseCode='400'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                ErrorResponseFormat = new ErrorResponseFormatConfig()
                {
                    ObligatoryErrorProperties    = "code,message",
                    NonObligatoryErrorProperties = "details,fields.name,fields.message,fields.value,fields.detail"
                },
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 20
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #25
0
        public PropertyStartingWithTypeRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"dblPropertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"parameters\" : [{\n            \"in\" : \"query\",\n            \"name\" : \"parameterOne\",\n            \"type\" : \"string\",\n            \"description\" : \"Too short, but ok.\",\n            \"required\" : true\n        }]\n        ,\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"intPropertyOne\" : {\n                      \"type\" : \"number\"\n                    },\n                    \"flagUpdated\" : {\n                      \"type\" : \"number\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-one',Operation='get',ResponseCode='200',PropertyFull='data.dblPropertyOne'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                PropertyStartingWithType = new PropertyStartingWithTypeConfig()
                {
                    WordsToAvoid = "bool,byte,char,dbl,decimal,double,flag,float,indicador,int,integer,long,obj,sbyte,str,string,uint,ulong,short,ushort"
                },
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 20
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #26
0
        public StringCouldBeNumberRuleTest()
        {
            const string contract = "{\r\n  \"swagger\" : \"2.0\",\r\n  \"info\" : {\r\n    \"title\": \"teste 2\",\r\n    \"version\": \"1.0\"\r\n  },\r\n  \"consumes\" : [ \"application/json\" ],\r\n  \"produces\" : [ \"application/json\" ],\r\n  \"paths\" : {\r\n    \"/path-one\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Status 200\",\r\n            \"schema\" : {\r\n              \"type\" : \"object\",\r\n              \"properties\" : {\r\n                \"one\" : {\r\n                  \"type\" : \"string\",\r\n                  \"example\": \"R$ 100.000,11\"\r\n                },\r\n                \"two\" : {\r\n                  \"type\" : \"string\",\r\n                  \"example\": \"020.22\"\r\n                },\r\n                \"three\" : {\r\n                  \"type\" : \"string\",\r\n                  \"example\": \"030,30\"\r\n                },\r\n                \"four\" : {\r\n                  \"type\" : \"string\",\r\n                  \"example\": \" 44 \"\r\n                },\r\n                \"five\" : {\r\n                  \"type\" : \"number\",\r\n                  \"example\": .5\r\n                },\r\n                \"anIp\" : {\r\n                  \"type\" : \"string\",\r\n                  \"example\": \"192.168.0.110\"\r\n                },\r\n                \"clientCpf\" : {\r\n                  \"type\" : \"string\",\r\n                  \"example\": \"33344455566\"\r\n                },\r\n                \"cnpjRaiz\" : {\r\n                  \"type\" : \"string\",\r\n                  \"example\": \"12345678\"\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    },\r\n    \"/path-two\" : {\r\n      \"get\" : {\r\n        \"responses\" : {\r\n          \"200\" : {\r\n            \"description\" : \"Status 200\",\r\n            \"schema\" : {\r\n              \"type\" : \"object\",\r\n              \"properties\" : {\r\n                \"data\" : {\r\n                  \"type\" : \"object\",\r\n                  \"properties\" : {\r\n                    \"six\" : {\r\n                      \"type\" : \"string\",\r\n                      \"example\": \"6\"\r\n                    },\r\n                    \"seven\" : {\r\n                      \"type\" : \"string\",\r\n                      \"example\": \"A77\"\r\n                    },\r\n                    \"anIp\" : {\r\n                      \"type\" : \"string\",\r\n                      \"example\": \"192.168.0.110\"\r\n                    }\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-two',Operation='get',ResponseCode='200',Parameter='data.six'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                StringCouldBeNumber = new StringCouldBeNumberConfig()
                {
                    CurrencySymbols = "$,.?,?,?.,?.?.,??,???,???.,¢,£,¥,€,AMD,Ar,AUD,B/.,BND,Br,Bs.,Bs.,Bs.S.,C$,CUC,D,Db,din.,Esc,ƒ,FOK,Fr,Fr.,Ft,G,GBP,GGP,INR,JOD,K,Kc,KM,kn,kr,Ks,Kz,L,Le,lei,m,MAD,MK,MRU,MT,Nfk,Nu.,NZD,P,PND,Q,R,R$,RM,Rp,Rs,RUB,S/.,SGD,Sh,Sl,so'm,T,T$,UM,USD,Vt,ZAR,ZK,zl",
                    ExceptionsRegex = @"(?i)(\W|^|\-)[\w.\-]{0,100}(cpf|cnpj)[\w.\-]{0,100}(\W|$|\-)"
                },
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 5
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #27
0
        public DescriptionRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"parameters\" : [{\n            \"in\" : \"query\",\n            \"name\" : \"parameterOne\",\n            \"type\" : \"string\",\n            \"description\" : \"too short\",\n            \"required\" : true\n        }]\n        ,\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression
                {
                    RuleName = ruleName,
                    Target   = "Path='/path-one',Operation='get',ResponseCode='200'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                Description = new DescriptionConfig()
                {
                    MinDescriptionLength       = 5,
                    MidDescriptionLength       = 15,
                    LargeDescriptionLength     = 20,
                    TestDescriptionInOperation = true,
                    TestDescriptionInPaths     = true
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #28
0
        public Http200WithoutPaginationRuleTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/path-one\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"array\",\n                  \"items\" : {\n                    \"properties\" : {\n                      \"intPropertyOne\" : {\n                        \"type\" : \"number\"\n                      },\n                      \"flagUpdated\" : {\n                        \"type\" : \"number\"\n                      }\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\" : {\n      \"get\" : {\n        \"parameters\" : [{\n            \"in\" : \"query\",\n            \"name\" : \"parameterOne\",\n            \"type\" : \"string\",\n            \"description\" : \"Too short, but ok.\",\n            \"required\" : true\n        }]\n        ,\n        \"responses\" : {\n          \"200\" : {\n            \"description\": \"\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"array\",\n                  \"items\" : {\n                    \"properties\" : {\n                      \"intPropertyOne\" : {\n                        \"type\" : \"number\"\n                      },\n                      \"flagUpdated\" : {\n                        \"type\" : \"number\"\n                      }\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/path-one'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            var settings = new RuleSettings()
            {
                Http200WithoutPagination = new Http200WithoutPaginationConfig()
                {
                    PaginationEnvelopeName = "pagination",
                    ContentEnvelopeName    = "data"
                },
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 20
                }
            };

            ruleSettings = Options.Create(settings);

            cache = new OpenApiDocumentCache();
        }
コード例 #29
0
        public PrepositionsTest()
        {
            const string contract =
                "{\n  \"swagger\" : \"2.0\",\n  \"info\" : {\n    \"version\" : \"v1\",\n    \"title\" : \"Swagger Test\",\n  },\n  \"consumes\" : [ \"application/json\" ],\n  \"produces\" : [ \"application/json\" ],\n  \"paths\" : {\n    \"/meios-de-pagamento\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Irá bater na regra\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"nome\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/paths-two\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Irá bater na regra\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"campoDePreposicao\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-three\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Não irá bater na regra\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"propertyOne\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-fours/{idAlgoDeDepois}\" : {\n      \"get\" : {\n         parameters: [{\n            \"name\": \"idAlgoDeDepois\",\n            \"in\": \"path\",\n            \"description\": \"ID\",\n            \"required\": true,\n            \"type\": \"integer\",\n            \"format\": \"int64\",\n         }],\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Irá bater na regra\"\n          }\n        }\n      }\n    },\n    \"/path-fives\" : {\n      \"get\" : {\n        parameters: [{\n            \"name\": \"nomeDaEmpresa\",\n            \"in\": \"query\",\n            \"description\": \"nome\",\n            \"required\": true,\n            \"type\": \"string\"\n         }],\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Irá bater na regra\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"array\",\n                  \"items\": {\n \t\t\t\t\t\"type\" : \"object\",\n                    \"properties\" : {\n                      \"propertyOne\" : {\n                    \t\"type\" : \"string\"\n                      }\n                  \t}\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/paths-six\" : {\n      \"get\" : {\n        \"responses\" : {\n          \"200\" : {\n            \"description\" : \"Irá bater na regra\",\n            \"schema\" : {\n              \"type\" : \"object\",\n              \"properties\" : {\n                \"data\" : {\n                  \"type\" : \"object\",\n                  \"properties\" : {\n                    \"cadastroAsEscuras\" : {\n                      \"type\" : \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            var settings = new RuleSettings()
            {
                Prepositions = new PrepositionsConfig()
                {
                    WordsToAvoid = "a,à,as,às,ao,aos,ante,aonde,apos,aquilo,com,contra,da,de,desde,dessa,dessas,desse,desses,desta,destas,deste,destes,disto,do,duma,e,em,entre,na,nas,no,nos,num,numa,numas,nuns,nessa,naquilo,nessas,nesse,nesses,nesta,nestas,neste,nestes,nisso,nisto,o,para,perante,pela,pelas,pelo,pelos,por,pra,pras,sem,sob,sobre,tras,trás",
                    Example      = "cartao-credito ao invés de cartao-de-credito, telefoneContato ao invés de telefoneParaContato"
                },
                NestingDepth = new NestingDepthConfig()
                {
                    Depth = 10
                }
            };

            ruleSettings = Options.Create(settings);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/paths-two',Operation='get'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            cache = new OpenApiDocumentCache();
        }
        public Http201WithoutContentLocationHeaderRuleTest()
        {
            const string contract =
                "{\n  \"swagger\": \"2.0\",\n  \"info\": {\n    \"title\": \"teste 2\",\n    \"version\": \"1.0\"\n  },\n  \"consumes\": [\n    \"application/json\"\n  ],\n  \"produces\": [\n    \"application/json\"\n  ],\n  \"paths\": {\n    \"/path-one\": {\n      \"post\": {\n        \"responses\": {\n          \"201\": {\n            \"description\": \"Status 201\",\n            \"schema\": {\n              \"type\": \"object\",\n              \"properties\": {\n                \"noDataEnvelope\": {\n                  \"type\": \"object\",\n                  \"properties\": {\n                    \"propertyOne\": {\n                      \"type\": \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    },\n    \"/path-two\": {\n      \"post\": {\n        \"responses\": {\n          \"201\": {\n            \"description\": \"Status 201\",\n            \"schema\": {\n              \"type\": \"object\",\n              \"properties\": {\n                \"noDataEnvelope\": {\n                  \"type\": \"object\",\n                  \"properties\": {\n                    \"propertyOne\": {\n                      \"type\": \"string\"\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n}";

            openApiContract = new OpenApiStringReader().Read(contract, out OpenApiDiagnostic _);

            supressions = new Supressions(new List <Supression>
            {
                new Supression {
                    RuleName = ruleName, Target = "Path='/path-one',Operation='post'"
                }
            });

            supressionEntireRule = new Supressions(
                new List <Supression> {
                new Supression {
                    RuleName = ruleName, Target = "*"
                }
            });

            cache = new OpenApiDocumentCache();
        }