diff --git a/README.md b/README.md index 5c7194d8..be266ba1 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ Thanks to our sponsors for supporting this project! ## What it can validate +### Structure +- At least one of `paths`, `components` or `webhooks` must be present (as of OpenAPI 3.1, a document describing only webhooks or only reusable components is valid) + ### API Info - API license must be present and non-empty string - API description must be present and non-empty string 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 06017d19..efd47fb4 100644 --- a/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ValidationInitiatorTest.java +++ b/cli/src/test/java/org/openapitools/openapistylevalidator/cli/ValidationInitiatorTest.java @@ -308,6 +308,26 @@ void shouldNotFailWhenThereAreNoPaths() throws Throwable { assertDoesNotThrow(() -> validationInitiator.validate(optionManager, commandLine)); } + + /** + * See https://github.com/OpenAPITools/openapi-style-validator/issues/93 + * + *

End-to-end: a webhooks-only 3.1 document passes the structural + * validation. The webhooks do not survive the parse to empoa + * conversion as MicroProfile fields - the wrapper keeps them behind + * its swagger model, where the lib finds them reflectively. + */ + @Test + void shouldNotReportErrorWhenThereAreOnlyWebhooks() throws ParseException { + CommandLine commandLine = parser.parse(options, new String[] { + "-s", "src/test/resources/webhooksOnly.yaml", + "-o", "src/test/resources/default.json" + }); + + List errorList = validationInitiator.validate(optionManager, commandLine); + + assertEquals(0, errorList.size()); + } } private void assertThrowsIllegalArgumentExceptionWithExceptedMessage( diff --git a/cli/src/test/resources/webhooksOnly.yaml b/cli/src/test/resources/webhooksOnly.yaml new file mode 100644 index 00000000..40e6e47e --- /dev/null +++ b/cli/src/test/resources/webhooksOnly.yaml @@ -0,0 +1,24 @@ +openapi: 3.1.0 +info: + title: Webhooks only specification + version: '1.0' + description: Example API with only webhooks + contact: + name: OpenAPI Tools + email: team@openapitools.org + license: + name: Eclipse Public License 2.0 + url: https://www.eclipse.org/legal/epl-2.0/ +servers: + - url: 'http://localhost:8080/' +webhooks: + onEvent: + post: + operationId: onEvent + summary: A simple post call + description: When this method is called, the server answers with 200 OKs + tags: + - demo + responses: + '200': + description: OK diff --git a/lib/build.gradle b/lib/build.gradle index 5ec0211a..c180608b 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -6,6 +6,7 @@ dependencies { implementation 'org.eclipse.microprofile.openapi:microprofile-openapi-api:3.1.1' testImplementation 'org.slf4j:slf4j-jdk14:2.0.16' testImplementation 'org.openapitools.empoa:empoa-simple-models-impl:2.1.0' + testImplementation 'io.swagger.core.v3:swagger-models:2.2.23' testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.16.2' } diff --git a/lib/src/main/java/org/openapitools/openapistylevalidator/ErrorAggregator.java b/lib/src/main/java/org/openapitools/openapistylevalidator/ErrorAggregator.java index 416518f0..3299c1e0 100644 --- a/lib/src/main/java/org/openapitools/openapistylevalidator/ErrorAggregator.java +++ b/lib/src/main/java/org/openapitools/openapistylevalidator/ErrorAggregator.java @@ -79,10 +79,10 @@ void logModelBadNaming(String variableName, String variableType, String neededNa model)); } - public void logMissingPathsAndComponents() { + public void logMissingPathsComponentsOrWebhooks() { errorList.add(new StyleError( StyleError.StyleCheckSection.OpenAPI, - "paths,components", - "Should have at least one of paths or components")); + "paths,components,webhooks", + "Should have at least one of paths, components or webhooks")); } } diff --git a/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java b/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java index 10a0671e..050e2eea 100644 --- a/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java +++ b/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java @@ -1,5 +1,6 @@ package org.openapitools.openapistylevalidator; +import java.lang.reflect.Method; import java.util.*; import org.eclipse.microprofile.openapi.models.OpenAPI; import org.eclipse.microprofile.openapi.models.Operation; @@ -41,11 +42,58 @@ public List validate(ValidatorParameters parameters) { private void validateStructure() { boolean hasNoPaths = openAPI.getPaths() == null; boolean hasNoComponents = openAPI.getComponents() == null; - if (hasNoPaths && hasNoComponents) { - errorAggregator.logMissingPathsAndComponents(); + boolean hasNoWebhooks = getWebhooks(openAPI) == null; + if (hasNoPaths && hasNoComponents && hasNoWebhooks) { + errorAggregator.logMissingPathsComponentsOrWebhooks(); } } + /* + * The `webhooks` field was introduced at the root of the document by + * OpenAPI 3.1.0, which requires at least one of `paths`, `components` or + * `webhooks` to exist: a document describing only webhooks, or only + * reusable components, is valid. + * + * The MicroProfile OpenAPI API this library is built against (3.1) + * predates it and declares no `webhooks` accessor: `getWebhooks()` + * exists only on models aligned with OAS 3.1, such as the MicroProfile + * OpenAPI 4.0 API. The CLI, Gradle and Maven plugins parse + * specifications with swagger-parser and convert the result through + * empoa-swagger-core, whose wrapper keeps the underlying swagger model - + * which does carry `webhooks` - behind its `getSw()` accessor. + * + * Both situations are handled reflectively so the check works with any + * of them; when neither is available, the document is considered to + * have no webhooks. + * See https://github.com/OpenAPITools/openapi-style-validator/issues/93 + */ + private static Map getWebhooks(OpenAPI openAPI) { + try { + Method getWebhooks = openAPI.getClass().getMethod("getWebhooks"); + return asMap(getWebhooks.invoke(openAPI)); + } catch (NoSuchMethodException e) { + // The model has no webhooks accessor: try the swagger model behind an empoa wrapper, if any + } catch (ReflectiveOperationException e) { + return null; + } + + try { + Method getSw = openAPI.getClass().getMethod("getSw"); + Object swaggerOpenAPI = getSw.invoke(openAPI); + if (swaggerOpenAPI == null) { + return null; + } + Method getWebhooks = swaggerOpenAPI.getClass().getMethod("getWebhooks"); + return asMap(getWebhooks.invoke(swaggerOpenAPI)); + } catch (ReflectiveOperationException e) { + return null; + } + } + + private static Map asMap(Object webhooks) { + return webhooks instanceof Map ? (Map) webhooks : null; + } + private void validateInfo() { Info info = openAPI.getInfo(); License license = info.getLicense(); diff --git a/lib/src/test/java/org/openapitools/openapistylevalidator/ErrorAggregatorTest.java b/lib/src/test/java/org/openapitools/openapistylevalidator/ErrorAggregatorTest.java index 4905dcd5..6b187e54 100644 --- a/lib/src/test/java/org/openapitools/openapistylevalidator/ErrorAggregatorTest.java +++ b/lib/src/test/java/org/openapitools/openapistylevalidator/ErrorAggregatorTest.java @@ -45,4 +45,15 @@ void logMissingModelProperty() { nameOfMissingProperty); assertEquals(Collections.singletonList(expectedError), errorAggregator.getErrorList()); } + + @Test + void logMissingPathsComponentsOrWebhooksShouldAddToErrorList() { + errorAggregator.logMissingPathsComponentsOrWebhooks(); + + Assertions.assertAll( + () -> assertEquals(1, errorAggregator.getErrorList().size()), + () -> assertEquals( + "*ERROR* Section: OpenAPI: 'paths,components,webhooks' -> Should have at least one of paths, components or webhooks", + errorAggregator.getErrorList().get(0).toString())); + } } diff --git a/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java b/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java index 31d088b7..ce52b817 100644 --- a/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java +++ b/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java @@ -259,10 +259,81 @@ void shouldReportMissingPathsOrComponents() { Assertions.assertAll( () -> assertEquals(1, errors.size()), () -> assertEquals( - "*ERROR* Section: OpenAPI: 'paths,components' -> Should have at least one of paths or components", + "*ERROR* Section: OpenAPI: 'paths,components,webhooks' -> Should have at least one of paths, components or webhooks", errors.get(0).toString())); } + /* + * See https://github.com/OpenAPITools/openapi-style-validator/issues/93 + * + * OpenAPI 3.1.0 requires at least one of `paths`, `components` or + * `webhooks` to exist: a document describing only webhooks is valid. + * + * The MicroProfile OpenAPI API the library is built against (3.1) has no + * `webhooks` accessor, so this fake provides one the way an OAS 3.1-aware + * model (e.g. MicroProfile OpenAPI 4.0) does. + */ + private static OpenAPI openAPIWithWebhooksAccessor( + Map webhooks) { + return new org.openapitools.empoa.simple.internal.models.OpenAPIImpl() { + @SuppressWarnings("unused") + public Map getWebhooks() { + return webhooks; + } + }.info(createValidOpenAPI().getInfo()); + } + + @Test + void shouldNotReportMissingPathsOrComponentsWhenThereAreWebhooks() { + OpenAPI openAPI = openAPIWithWebhooksAccessor(Collections.singletonMap("onEvent", OASFactory.createPathItem())) + .paths(null) + .components(null); + + OpenApiSpecStyleValidator validator = new OpenApiSpecStyleValidator(openAPI); + + List errors = validator.validate(new ValidatorParameters()); + assertEquals(0, errors.size()); + } + + /* + * Like `paths` and `components`, declaring the field with an empty + * object counts as present: the structural check reports documents that + * declare none of the three at all. + */ + @Test + void shouldNotReportMissingPathsOrComponentsWhenWebhooksMapIsEmpty() { + OpenAPI openAPI = + openAPIWithWebhooksAccessor(Collections.emptyMap()).paths(null).components(null); + + OpenApiSpecStyleValidator validator = new OpenApiSpecStyleValidator(openAPI); + + List errors = validator.validate(new ValidatorParameters()); + assertEquals(0, errors.size()); + } + + /* + * The CLI, Gradle and Maven plugins convert the parsed specification to + * MicroProfile models through empoa-swagger-core: the document is a + * wrapper around the swagger model, which carries `webhooks` even though + * the wrapper does not expose it. + */ + @Test + void shouldNotReportMissingPathsOrComponentsWhenWebhooksAreBehindSwaggerModel() { + io.swagger.v3.oas.models.OpenAPI swaggerOpenAPI = new io.swagger.v3.oas.models.OpenAPI(); + swaggerOpenAPI.webhooks(Collections.singletonMap("onEvent", new io.swagger.v3.oas.models.PathItem())); + OpenAPI openAPI = new org.openapitools.empoa.simple.internal.models.OpenAPIImpl() { + @SuppressWarnings("unused") + public io.swagger.v3.oas.models.OpenAPI getSw() { + return swaggerOpenAPI; + } + }.info(createValidOpenAPI().getInfo()).paths(null).components(null); + + OpenApiSpecStyleValidator validator = new OpenApiSpecStyleValidator(openAPI); + + List errors = validator.validate(new ValidatorParameters()); + assertEquals(0, errors.size()); + } + /* tests for naming convention parameters */ @Test void shouldReportPathNamingConventionError() {