Skip to content

Commit 712e863

Browse files
committed
Added new api module to support custom rule and ported 3 existing rules into new design. I
1 parent 13db6fb commit 712e863

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+420
-246
lines changed

api/build.gradle

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
plugins {
2+
id "com.diffplug.spotless" version "6.9.1"
3+
}
4+
5+
dependencies {
6+
implementation 'org.eclipse.microprofile.openapi:microprofile-openapi-api:3.0'
7+
testImplementation 'org.slf4j:slf4j-jdk14:2.0.0'
8+
testImplementation 'org.openapitools.empoa:empoa-simple-models-impl:2.0.0'
9+
testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.10.1'
10+
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
11+
implementation group: 'org.reflections', name: 'reflections', version: '0.10.2'
12+
}
13+
14+
java {
15+
withJavadocJar()
16+
withSourcesJar()
17+
}
18+
19+
spotless {
20+
java {
21+
removeUnusedImports()
22+
trimTrailingWhitespace()
23+
palantirJavaFormat()
24+
}
25+
}
26+
27+
publishing {
28+
publications {
29+
mavenJava(MavenPublication) {
30+
artifactId = 'openapi-style-validator-api'
31+
pom {
32+
name = 'OpenAPI Style Validator - api'
33+
description = 'module to expose API for writing rules to validate the style and standards of an OpenAPI specification'
34+
packaging = 'jar'
35+
url = 'https://openapitools.github.io/openapi-style-validator/'
36+
inceptionYear = "2019"
37+
licenses {
38+
license {
39+
name = 'Apache 2.0 License'
40+
url = 'http://www.apache.org/licenses/LICENSE-2.0'
41+
}
42+
}
43+
developers {
44+
developer {
45+
id = 'JFCote'
46+
name = 'Jean-Francois Cote'
47+
email = 'jcote@stingray.com'
48+
}
49+
}
50+
scm {
51+
connection = 'scm:git:https://github.com/openapitools/openapi-style-validator.git'
52+
developerConnection = 'scm:git:https://github.com/openapitools/openapi-style-validator.git'
53+
url = 'https://github.com/openapitools/openapi-style-validator/'
54+
}
55+
}
56+
from components.java
57+
}
58+
}
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.openapitools.openapistylevalidator.api;
2+
3+
import java.util.Optional;
4+
import org.eclipse.microprofile.openapi.models.OpenAPI;
5+
import org.openapitools.openapistylevalidator.error.StyleError;
6+
7+
public interface Rule {
8+
9+
String ruleName();
10+
11+
String description();
12+
13+
Optional<StyleError> execute(OpenAPI api);
14+
}

lib/src/main/java/org/openapitools/openapistylevalidator/styleerror/GenericStyleError.java renamed to api/src/main/java/org/openapitools/openapistylevalidator/error/GenericStyleError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.openapitools.openapistylevalidator.styleerror;
1+
package org.openapitools.openapistylevalidator.error;
22

33
import java.util.Objects;
44

lib/src/main/java/org/openapitools/openapistylevalidator/styleerror/ModelNamingStyleError.java renamed to api/src/main/java/org/openapitools/openapistylevalidator/error/ModelNamingStyleError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.openapitools.openapistylevalidator.styleerror;
1+
package org.openapitools.openapistylevalidator.error;
22

33
import java.util.Objects;
44

lib/src/main/java/org/openapitools/openapistylevalidator/styleerror/ModelStyleError.java renamed to api/src/main/java/org/openapitools/openapistylevalidator/error/ModelStyleError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.openapitools.openapistylevalidator.styleerror;
1+
package org.openapitools.openapistylevalidator.error;
22

33
import java.util.Objects;
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.openapitools.openapistylevalidator.styleerror;
1+
package org.openapitools.openapistylevalidator.error;
22

33
import java.util.Objects;
44
import org.eclipse.microprofile.openapi.models.PathItem;

lib/src/main/java/org/openapitools/openapistylevalidator/styleerror/OperationStyleError.java renamed to api/src/main/java/org/openapitools/openapistylevalidator/error/OperationStyleError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.openapitools.openapistylevalidator.styleerror;
1+
package org.openapitools.openapistylevalidator.error;
22

33
import java.util.Objects;
44
import org.eclipse.microprofile.openapi.models.PathItem;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.openapitools.openapistylevalidator.error;
2+
3+
public enum StyleCheckSection {
4+
APIInfo,
5+
Operations,
6+
Models,
7+
Naming,
8+
OpenAPI,
9+
}

lib/src/main/java/org/openapitools/openapistylevalidator/styleerror/StyleError.java renamed to api/src/main/java/org/openapitools/openapistylevalidator/error/StyleError.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
package org.openapitools.openapistylevalidator.styleerror;
1+
package org.openapitools.openapistylevalidator.error;
22

33
public class StyleError {
44

5-
public enum StyleCheckSection {
6-
APIInfo,
7-
Operations,
8-
Models,
9-
Naming,
10-
OpenAPI,
11-
}
12-
135
final StyleCheckSection styleCheckSection;
146
final String fieldNames;
157
final String description;

cli/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ apply plugin: 'application'
66

77
dependencies {
88
implementation project(':openapi-style-validator-lib')
9+
implementation project(':openapi-style-validator-api')
910
implementation 'commons-cli:commons-cli:1.5.0'
1011
implementation 'org.slf4j:slf4j-jdk14:2.0.0'
1112
implementation 'io.swagger.parser.v3:swagger-parser:2.0.33'

cli/src/main/java/org/openapitools/openapistylevalidator/cli/OutputUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.apache.commons.cli.Options;
1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13-
import org.openapitools.openapistylevalidator.styleerror.StyleError;
13+
import org.openapitools.openapistylevalidator.error.StyleError;
1414

1515
class OutputUtils {
1616
private static final Logger logger = LogManager.getLogger(OutputUtils.class);

cli/src/main/java/org/openapitools/openapistylevalidator/cli/ValidationInitiator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import org.apache.commons.cli.CommandLine;
88
import org.eclipse.microprofile.openapi.models.OpenAPI;
99
import org.openapitools.empoa.swagger.core.internal.SwAdapter;
10+
import org.openapitools.openapistylevalidator.OpenAPIStyleValidator;
1011
import org.openapitools.openapistylevalidator.OpenApiSpecStyleValidator;
1112
import org.openapitools.openapistylevalidator.ValidatorParameters;
12-
import org.openapitools.openapistylevalidator.styleerror.StyleError;
13+
import org.openapitools.openapistylevalidator.error.StyleError;
1314

1415
public class ValidationInitiator {
1516
public List<StyleError> validate(OptionManager optionManager, CommandLine commandLine) {
@@ -31,4 +32,11 @@ private OpenAPI parseToOpenAPIModels(OptionManager optionManager, CommandLine co
3132
io.swagger.v3.oas.models.OpenAPI swaggerOpenAPI = parserResult.getOpenAPI();
3233
return SwAdapter.toOpenAPI(swaggerOpenAPI);
3334
}
35+
36+
public List<StyleError> validateV2(OptionManager optionManager, CommandLine commandLine) {
37+
OpenAPI openAPI = parseToOpenAPIModels(optionManager, commandLine);
38+
ValidatorParameters parameters = optionManager.getOptionalValidatorParametersOrDefault(commandLine);
39+
OpenAPIStyleValidator openAPIStyleValidator = new OpenAPIStyleValidator(openAPI, parameters);
40+
return openAPIStyleValidator.validate();
41+
}
3442
}

cli/src/main/java/rules/CheckTagsAreDefinedRule.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

cli/src/main/java/rules/OperationHasTagRule.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

cli/src/test/java/org/openapitools/openapistylevalidator/cli/ValidationInitiatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.apache.commons.cli.ParseException;
1010
import org.junit.jupiter.api.*;
1111
import org.mockito.Mockito;
12-
import org.openapitools.openapistylevalidator.styleerror.StyleError;
12+
import org.openapitools.openapistylevalidator.error.StyleError;
1313
import org.opentest4j.MultipleFailuresError;
1414

1515
class ValidationInitiatorTest {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.openapitools.openapistylevalidator.cli;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
36
import org.apache.commons.cli.CommandLine;
47
import org.apache.commons.cli.DefaultParser;
58
import org.apache.commons.cli.Options;
@@ -10,18 +13,13 @@
1013
import org.junit.jupiter.api.Nested;
1114
import org.junit.jupiter.api.Test;
1215
import org.mockito.Mockito;
13-
import org.openapitools.openapistylevalidator.styleerror.StyleError;
16+
import org.openapitools.openapistylevalidator.error.StyleError;
1417
import org.opentest4j.MultipleFailuresError;
1518

16-
import java.util.List;
17-
18-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
19-
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
import static org.junit.jupiter.api.Assertions.assertThrows;
21-
22-
class CustomRuleValidatorTest {
19+
class ValidationInitiatorV2Test {
2320
private static final DefaultParser parser = new DefaultParser();
24-
public static final String SRC_TEST_RESOURCES_MISSING_TAG_YAML = "src/test/resources/missing_tags.yaml";
21+
22+
public static final String SRC_TEST_RESOURCES_PING_YAML = "src/test/resources/ping.yaml";
2523

2624
private final OutputUtils outputUtils = Mockito.mock(OutputUtils.class);
2725
private OptionManager optionManager;
@@ -45,21 +43,20 @@ class Validate {
4543
class GivenFileProvidedWithInvalidTagDefinedYaml {
4644
@BeforeEach
4745
void init() throws ParseException {
48-
commandLine = parser.parse(options, new String[] {"-s", SRC_TEST_RESOURCES_MISSING_TAG_YAML});
46+
commandLine = parser.parse(options, new String[] {"-s", SRC_TEST_RESOURCES_PING_YAML});
4947
}
5048

5149
@Test
5250
void returnSevenErrors() {
53-
List<StyleError> errorList = validationInitiator.validate(optionManager, commandLine);
54-
55-
sevenErrorsAssertions(errorList);
51+
List<StyleError> errorList = validationInitiator.validateV2(optionManager, commandLine);
52+
fiveErrorsAssertions(errorList);
5653
}
5754
}
5855
}
5956

60-
private void sevenErrorsAssertions(List<StyleError> errorList) throws MultipleFailuresError {
57+
private void fiveErrorsAssertions(List<StyleError> errorList) throws MultipleFailuresError {
6158
Assertions.assertAll(
62-
() -> assertEquals(7, errorList.size()),
59+
() -> assertEquals(3, errorList.size()),
6360
() -> assertEquals(
6461
"*ERROR* Section: APIInfo: 'license' -> Should be present and not empty",
6562
errorList.get(0).toString()),
@@ -68,22 +65,6 @@ private void sevenErrorsAssertions(List<StyleError> errorList) throws MultipleFa
6865
errorList.get(1).toString()),
6966
() -> assertEquals(
7067
"*ERROR* Section: APIInfo: 'contact' -> Should be present and not empty",
71-
errorList.get(2).toString()),
72-
() -> assertEquals(
73-
"*ERROR* in Operation POST /ping 'description' -> This field should be present and not empty",
74-
errorList.get(3).toString()),
75-
() -> assertEquals(
76-
"*ERROR* in Operation POST /ping 'summary' -> This field should be present and not empty",
77-
errorList.get(4).toString()),
78-
() -> assertEquals(
79-
"*ERROR* Section: Operations: 'tags' -> FIX ME is not defined under tag section",
80-
errorList.get(5).toString()),
81-
82-
() -> assertEquals(
83-
"*ERROR* Section: Operations: 'tags' -> FIX ME is not defined under tag section",
84-
errorList.get(5).toString()),
85-
() -> assertEquals(
86-
"*ERROR* Section: OpenAPI: 'tags' -> Should be present and not empty",
87-
errorList.get(6).toString()));
68+
errorList.get(2).toString()));
8869
}
8970
}

cli/src/test/resources/missing_tags.yaml

Lines changed: 0 additions & 14 deletions
This file was deleted.

gradle-plugin/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44

55
dependencies {
66
implementation project(':openapi-style-validator-lib')
7+
implementation project(':openapi-style-validator-api')
78
implementation 'io.swagger.parser.v3:swagger-parser:2.0.33'
89
implementation 'org.openapitools.empoa:empoa-swagger-core:2.0.0'
910
testImplementation 'junit:junit:4.13.2'

gradle-plugin/src/main/java/org/openapitools/openapistylevalidator/gradle/OpenAPIStyleValidatorTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.openapitools.openapistylevalidator.OpenApiSpecStyleValidator;
1212
import org.openapitools.openapistylevalidator.ValidatorParameters;
1313
import org.openapitools.openapistylevalidator.ValidatorParameters.NamingConvention;
14-
import org.openapitools.openapistylevalidator.styleerror.StyleError;
14+
import org.openapitools.openapistylevalidator.error.StyleError;
1515

1616
import java.util.List;
1717

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ plugins {
33
}
44

55
dependencies {
6+
implementation project(':openapi-style-validator-api')
67
implementation 'org.eclipse.microprofile.openapi:microprofile-openapi-api:3.0'
78
testImplementation 'org.slf4j:slf4j-jdk14:2.0.0'
89
testImplementation 'org.openapitools.empoa:empoa-simple-models-impl:2.0.0'

0 commit comments

Comments
 (0)