diff --git a/README.md b/README.md index 889e2771f..6a2c6efb3 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ The options file is described in json (example in `specs/options.json`), and has |queryParamNamingConvention|string| `CamelCase`, `PascalCase`, `HyphenUpperCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase` |(same as `parameterNamingConvention`)|Specific naming convention for query parameters [(note)](#parameter-naming-convention-hierarchy)| |cookieParamNamingConvention|string| `CamelCase`, `PascalCase`, `HyphenUpperCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase` |(same as `parameterNamingConvention`)|Specific naming convention for cookie parameters [(note)](#parameter-naming-convention-hierarchy)| |headerNamingConvention|string| `CamelCase`, `PascalCase`, `HyphenUpperCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase` |`UnderscoreUpperCase`|Naming convention for headers| +|schemaNamingConvention|string| `CamelCase`, `PascalCase`, `HyphenUpperCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase` |`PascalCase`|Naming convention for schemas| |propertyNamingConvention|string| `CamelCase`, `PascalCase`, `HyphenUpperCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase` |`CamelCase`|Naming convention for properties| |allowedModelProperties|array| `["_links", "_embedded"]`|`[]`|An array of property names that are authorised even if they do not match to the "propertyNamingConvention"| diff --git a/cli/src/main/java/org/openapitools/openapistylevalidator/cli/OptionManager.java b/cli/src/main/java/org/openapitools/openapistylevalidator/cli/OptionManager.java index 82f2db34c..701792852 100644 --- a/cli/src/main/java/org/openapitools/openapistylevalidator/cli/OptionManager.java +++ b/cli/src/main/java/org/openapitools/openapistylevalidator/cli/OptionManager.java @@ -99,6 +99,7 @@ private void validateNamingConventions(ValidatorParameters parameters) { validateNamingConvention("header", parameters.getHeaderNamingConvention()); validateNamingConvention("parameter", parameters.getParameterNamingConvention()); validateNamingConvention("path", parameters.getPathNamingConvention()); + validateNamingConvention("schema", parameters.getSchemaNamingConvention()); validateNamingConvention("property", parameters.getPropertyNamingConvention()); } diff --git a/cli/src/test/java/org/openapitools/openapistylevalidator/cli/OptionManagerTest.java b/cli/src/test/java/org/openapitools/openapistylevalidator/cli/OptionManagerTest.java index 87a075dd1..472400795 100644 --- a/cli/src/test/java/org/openapitools/openapistylevalidator/cli/OptionManagerTest.java +++ b/cli/src/test/java/org/openapitools/openapistylevalidator/cli/OptionManagerTest.java @@ -129,6 +129,8 @@ void shouldParseAllOptions() throws Exception { ValidatorParameters.NamingConvention.UnderscoreUpperCase, parameters.getHeaderNamingConvention()); Assertions.assertEquals( ValidatorParameters.NamingConvention.CamelCase, parameters.getParameterNamingConvention()); + Assertions.assertEquals( + ValidatorParameters.NamingConvention.PascalCase, parameters.getSchemaNamingConvention()); Assertions.assertEquals( ValidatorParameters.NamingConvention.CamelCase, parameters.getPropertyNamingConvention()); } diff --git a/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ResourceTest.java b/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ResourceTest.java index cd1ee1855..49afeb534 100644 --- a/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ResourceTest.java +++ b/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ResourceTest.java @@ -103,6 +103,11 @@ void defaultJsonFileContainsConstants() throws Exception { + ValidatorParameters.NamingConvention.CamelCase + NEXT_LINE_QUOTE + PREFIX + + ValidatorParameters.SCHEMA_NAMING_CONVENTION + + SEPARATOR_QUOTE + + ValidatorParameters.NamingConvention.PascalCase + + NEXT_LINE_QUOTE + + PREFIX + ValidatorParameters.PROPERTY_NAMING_CONVENTION + SEPARATOR_QUOTE + ValidatorParameters.NamingConvention.CamelCase diff --git a/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ValidationInitiatorTest.java b/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ValidationInitiatorTest.java index 1408ef434..276b4676f 100644 --- a/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ValidationInitiatorTest.java +++ b/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ValidationInitiatorTest.java @@ -247,6 +247,21 @@ void throwsIllegalArgumentException() { } } + @Nested + class GivenInvalidSchemaNamingConvention { + @BeforeEach + void init() throws ParseException { + commandLine = parser.parse(options, new String[] { + "-s", SRC_TEST_RESOURCES_SOME_YAML, "-o", "src/test/resources/invalidSchemaNamingConvention.json" + }); + } + + @Test + void throwsIllegalArgumentException() { + assertThrowsIllegalArgumentExceptionWithExceptedMessage("Invalid schemaNamingConvention", commandLine); + } + } + @Nested class GivenInvalidPropertyNamingConvention { @BeforeEach diff --git a/cli/src/test/resources/default.json b/cli/src/test/resources/default.json index 1173c500c..4b2dead8b 100644 --- a/cli/src/test/resources/default.json +++ b/cli/src/test/resources/default.json @@ -15,5 +15,6 @@ "pathNamingConvention": "HyphenCase", "headerNamingConvention": "UnderscoreUpperCase", "parameterNamingConvention": "CamelCase", + "schemaNamingConvention": "PascalCase", "propertyNamingConvention": "CamelCase" } \ No newline at end of file diff --git a/cli/src/test/resources/invalidSchemaNamingConvention.json b/cli/src/test/resources/invalidSchemaNamingConvention.json new file mode 100644 index 000000000..d115f37e3 --- /dev/null +++ b/cli/src/test/resources/invalidSchemaNamingConvention.json @@ -0,0 +1,3 @@ +{ + "schemaNamingConvention": "Wrong naming convention" +} diff --git a/gradle-plugin/src/main/java/org/openapitools/openapistylevalidator/gradle/OpenAPIStyleValidatorTask.java b/gradle-plugin/src/main/java/org/openapitools/openapistylevalidator/gradle/OpenAPIStyleValidatorTask.java index 40f17f841..8fa553649 100644 --- a/gradle-plugin/src/main/java/org/openapitools/openapistylevalidator/gradle/OpenAPIStyleValidatorTask.java +++ b/gradle-plugin/src/main/java/org/openapitools/openapistylevalidator/gradle/OpenAPIStyleValidatorTask.java @@ -38,6 +38,7 @@ public class OpenAPIStyleValidatorTask extends DefaultTask { private NamingConvention pathNamingConvention = NamingConvention.HyphenCase; private NamingConvention parameterNamingConvention = NamingConvention.CamelCase; private NamingConvention headerNamingConvention = NamingConvention.UnderscoreUpperCase; + private NamingConvention schemaNamingConvention = NamingConvention.PascalCase; private NamingConvention propertyNamingConvention = NamingConvention.CamelCase; private NamingConvention queryParamNamingConvention = NamingConvention.CamelCase; private NamingConvention pathParamNamingConvention = NamingConvention.CamelCase; @@ -160,22 +161,27 @@ public void setHeaderNamingConvention(NamingConvention headerNamingConvention) { this.headerNamingConvention = headerNamingConvention; } + @Option(option = ValidatorParameters.SCHEMA_NAMING_CONVENTION, description = "Naming convention for schemas") + public void setSchemaNamingConvention(NamingConvention schemaNamingConvention) { + this.schemaNamingConvention = schemaNamingConvention; + } + @Option(option = ValidatorParameters.PROPERTY_NAMING_CONVENTION, description = "Naming convention for properties") public void setPropertyNamingConvention(NamingConvention propertyNamingConvention) { this.propertyNamingConvention = propertyNamingConvention; } - @Option(option = ValidatorParameters.PROPERTY_NAMING_CONVENTION, description = "Naming convention for path parameters") + @Option(option = ValidatorParameters.PATH_PARAM_NAMING_CONVENTION, description = "Naming convention for path parameters") public void setPathParamNamingConvention(NamingConvention pathParamNamingConvention) { this.pathParamNamingConvention = pathParamNamingConvention; } - @Option(option = ValidatorParameters.PROPERTY_NAMING_CONVENTION, description = "Naming convention for query parameters") + @Option(option = ValidatorParameters.QUERY_PARAM_NAMING_CONVENTION, description = "Naming convention for query parameters") public void setQueryParamNamingConvention(NamingConvention queryParamNamingConvention) { this.queryParamNamingConvention = queryParamNamingConvention; } - @Option(option = ValidatorParameters.PROPERTY_NAMING_CONVENTION, description = "Naming convention for cookie parameters") + @Option(option = ValidatorParameters.COOKIE_PARAM_NAMING_CONVENTION, description = "Naming convention for cookie parameters") public void setCookieParamNamingConvention(NamingConvention cookieParamNamingConvention) { this.cookieParamNamingConvention = cookieParamNamingConvention; } @@ -198,6 +204,7 @@ public ValidatorParameters createValidatorParameters() { parameters.setPathNamingConvention(pathNamingConvention); parameters.setParameterNamingConvention(parameterNamingConvention); parameters.setHeaderNamingConvention(headerNamingConvention); + parameters.setSchemaNamingConvention(schemaNamingConvention); parameters.setPropertyNamingConvention(propertyNamingConvention); parameters.setPathParamNamingConvention(pathParamNamingConvention); parameters.setQueryParamNamingConvention(queryParamNamingConvention); diff --git a/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java b/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java index d5a9c4332..5ef9ebf74 100644 --- a/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java +++ b/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java @@ -189,6 +189,15 @@ private void validateNaming() { if (openAPI.getComponents() != null && openAPI.getComponents().getSchemas() != null) { for (String definition : openAPI.getComponents().getSchemas().keySet()) { Schema model = openAPI.getComponents().getSchemas().get(definition); + boolean isSchemaNameValid = + namingValidator.isNamingValid(definition, parameters.getSchemaNamingConvention()); + if (!isSchemaNameValid) { + errorAggregator.logModelBadNaming( + definition, + "schema", + parameters.getSchemaNamingConvention().getDesignation(), + definition); + } if (model.getProperties() != null) { for (Map.Entry entry : diff --git a/lib/src/main/java/org/openapitools/openapistylevalidator/ValidatorParameters.java b/lib/src/main/java/org/openapitools/openapistylevalidator/ValidatorParameters.java index 859da5c7c..2c234003c 100644 --- a/lib/src/main/java/org/openapitools/openapistylevalidator/ValidatorParameters.java +++ b/lib/src/main/java/org/openapitools/openapistylevalidator/ValidatorParameters.java @@ -25,6 +25,7 @@ public class ValidatorParameters { public static final String PATH_NAMING_CONVENTION = "pathNamingConvention"; public static final String PARAMETER_NAMING_CONVENTION = "parameterNamingConvention"; public static final String HEADER_NAMING_CONVENTION = "headerNamingConvention"; + public static final String SCHEMA_NAMING_CONVENTION = "schemaNamingConvention"; public static final String PROPERTY_NAMING_CONVENTION = "propertyNamingConvention"; public static final String QUERY_PARAM_NAMING_CONVENTION = "queryParamNamingConvention"; public static final String PATH_PARAM_NAMING_CONVENTION = "pathParamNamingConvention"; @@ -72,6 +73,7 @@ public String getDesignation() { private NamingConvention pathNamingConvention = NamingConvention.HyphenCase; private NamingConvention parameterNamingConvention = NamingConvention.CamelCase; private NamingConvention headerNamingConvention = NamingConvention.UnderscoreUpperCase; + private NamingConvention schemaNamingConvention = NamingConvention.PascalCase; private NamingConvention propertyNamingConvention = NamingConvention.CamelCase; private NamingConvention queryParamNamingConvention = NamingConvention.CamelCase; private NamingConvention pathParamNamingConvention = NamingConvention.CamelCase; @@ -143,6 +145,10 @@ public NamingConvention getHeaderNamingConvention() { return headerNamingConvention; } + public NamingConvention getSchemaNamingConvention() { + return schemaNamingConvention; + } + public NamingConvention getPropertyNamingConvention() { return propertyNamingConvention; } @@ -243,6 +249,11 @@ public ValidatorParameters setHeaderNamingConvention(NamingConvention headerNami return this; } + public ValidatorParameters setSchemaNamingConvention(NamingConvention schemaNamingConvention) { + this.schemaNamingConvention = schemaNamingConvention; + return this; + } + public ValidatorParameters setPropertyNamingConvention(NamingConvention propertyNamingConvention) { this.propertyNamingConvention = propertyNamingConvention; return this; @@ -311,6 +322,7 @@ public String toString() { + "pathNamingConvention=%s, " + "headerNamingConvention=%s, " + "parameterNamingConvention=%s, " + + "schemaNamingConvention=%s, " + "propertyNamingConvention=%s, " + "queryParamNamingConvention=%s, " + "pathParamNamingConvention=%s, " @@ -332,6 +344,7 @@ public String toString() { pathNamingConvention, headerNamingConvention, parameterNamingConvention, + schemaNamingConvention, propertyNamingConvention, queryParamNamingConvention, pathParamNamingConvention, diff --git a/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java b/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java index adce6c5fc..81d44930c 100644 --- a/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java +++ b/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java @@ -331,6 +331,25 @@ void shouldReportHeaderNamingConventionError() { errors.get(0).toString())); } + @Test + void shouldReportSchemaNamingConventionError() { + OpenAPI openAPI = createValidOpenAPI(); + + OpenApiSpecStyleValidator validator = new OpenApiSpecStyleValidator(openAPI); + + ValidatorParameters validatorParams = new ValidatorParameters(); + validatorParams.setSchemaNamingConvention(NamingConvention.HyphenUpperCase); + List errors = validator.validate(validatorParams); + Assertions.assertAll( + () -> assertEquals(2, errors.size()), + () -> assertEquals( + "*ERROR* in model FooSchema 'FooSchema' -> schema should be in Hyphen-Upper-Case", + errors.get(0).toString()), + () -> assertEquals( + "*ERROR* in model BazSchema 'BazSchema' -> schema should be in Hyphen-Upper-Case", + errors.get(1).toString())); + } + @Test void shouldReportPropertyNamingConventionError() { OpenAPI openAPI = createValidOpenAPI(); diff --git a/lib/src/test/java/org/openapitools/openapistylevalidator/ValidatorParametersTest.java b/lib/src/test/java/org/openapitools/openapistylevalidator/ValidatorParametersTest.java index b7f25eb94..9b5fbb3e5 100644 --- a/lib/src/test/java/org/openapitools/openapistylevalidator/ValidatorParametersTest.java +++ b/lib/src/test/java/org/openapitools/openapistylevalidator/ValidatorParametersTest.java @@ -38,6 +38,8 @@ void validateDefaultValues() { () -> assertEquals( ValidatorParameters.NamingConvention.UnderscoreUpperCase, parameters.getHeaderNamingConvention()), + () -> assertEquals( + ValidatorParameters.NamingConvention.PascalCase, parameters.getSchemaNamingConvention()), () -> assertEquals( ValidatorParameters.NamingConvention.CamelCase, parameters.getPropertyNamingConvention()), () -> assertEquals( @@ -55,6 +57,7 @@ void testAllGetterSetters() { .setPathNamingConvention(ValidatorParameters.NamingConvention.CamelCase) .setParameterNamingConvention(ValidatorParameters.NamingConvention.CamelCase) .setHeaderNamingConvention(ValidatorParameters.NamingConvention.UnderscoreUpperCase) + .setSchemaNamingConvention(ValidatorParameters.NamingConvention.PascalCase) .setPropertyNamingConvention(ValidatorParameters.NamingConvention.CamelCase) .setQueryParamNamingConvention(ValidatorParameters.NamingConvention.CamelCase) .setPathParamNamingConvention(ValidatorParameters.NamingConvention.CamelCase) @@ -81,6 +84,8 @@ void testAllGetterSetters() { () -> assertEquals( ValidatorParameters.NamingConvention.UnderscoreUpperCase, parameters.getHeaderNamingConvention()), + () -> assertEquals( + ValidatorParameters.NamingConvention.PascalCase, parameters.getSchemaNamingConvention()), () -> assertEquals( ValidatorParameters.NamingConvention.CamelCase, parameters.getPropertyNamingConvention()), () -> assertEquals( diff --git a/maven-plugin/src/main/java/org/openapitools/openapistylevalidator/maven/OpenAPIStyleValidatorMojo.java b/maven-plugin/src/main/java/org/openapitools/openapistylevalidator/maven/OpenAPIStyleValidatorMojo.java index 85b201908..4f703ed0b 100644 --- a/maven-plugin/src/main/java/org/openapitools/openapistylevalidator/maven/OpenAPIStyleValidatorMojo.java +++ b/maven-plugin/src/main/java/org/openapitools/openapistylevalidator/maven/OpenAPIStyleValidatorMojo.java @@ -71,6 +71,9 @@ public class OpenAPIStyleValidatorMojo extends AbstractMojo { @Parameter(property = ValidatorParameters.HEADER_NAMING_CONVENTION, defaultValue = "UnderscoreUpperCase") private NamingConvention headerNamingConvention = NamingConvention.UnderscoreUpperCase; + @Parameter(property = ValidatorParameters.SCHEMA_NAMING_CONVENTION, defaultValue = "PascalCase") + private NamingConvention schemaNamingConvention = NamingConvention.PascalCase; + @Parameter(property = ValidatorParameters.PROPERTY_NAMING_CONVENTION, defaultValue = "CamelCase") private NamingConvention propertyNamingConvention = NamingConvention.CamelCase; @@ -128,6 +131,7 @@ public ValidatorParameters createValidatorParameters() { parameters.setPathNamingConvention(pathNamingConvention); parameters.setHeaderNamingConvention(headerNamingConvention); parameters.setParameterNamingConvention(parameterNamingConvention); + parameters.setSchemaNamingConvention(schemaNamingConvention); parameters.setPropertyNamingConvention(propertyNamingConvention); parameters.setPathParamNamingConvention(pathParamNamingConvention); parameters.setQueryParamNamingConvention(queryParamNamingConvention); diff --git a/maven-plugin/src/test/resources/projects/testDefault/pom.xml b/maven-plugin/src/test/resources/projects/testDefault/pom.xml index 31f5e2c4b..5e098e5a3 100644 --- a/maven-plugin/src/test/resources/projects/testDefault/pom.xml +++ b/maven-plugin/src/test/resources/projects/testDefault/pom.xml @@ -30,6 +30,7 @@ HyphenCase UnderscoreUpperCase CamelCase + PascalCase CamelCase @@ -43,4 +44,4 @@ - \ No newline at end of file + diff --git a/specs/options.json b/specs/options.json index c38262d57..e5132a999 100644 --- a/specs/options.json +++ b/specs/options.json @@ -17,8 +17,9 @@ "ignoreHeaderXNaming": true, "pathNamingConvention": "HyphenCase", "parameterNamingConvention": "CamelCase", + "schemaNamingConvention": "PascalCase", "propertyNamingConvention": "CamelCase", "queryParamNamingConvention": "CamelCase", - "pathParamNamingConvention": "HyphenCase", - "cookieParamNamingConvention": "CamelCase" + "pathParamNamingConvention": "HyphenCase", + "cookieParamNamingConvention": "CamelCase" }