From 9b3b1db81809971ca0cfc8b7a8c25bb9d98a1547 Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Sun, 15 Nov 2026 19:11:29 +0300 Subject: [PATCH 1/2] fix: recognize webhooks in the top-level structure check What: OpenAPI 3.1.0 requires at least one of `paths`, `components` or `webhooks` to exist at the top level of a document: a specification describing only webhooks, or only reusable components, is valid. The structural check flagged documents that declare neither `paths` nor `components` without considering `webhooks`, so a webhooks-only document was reported in error. The check now also treats the document as non-empty when webhooks are declared, and the message names the three alternatives. The MicroProfile OpenAPI API the library is built against (3.1) declares no `webhooks` accessor, so webhooks are read reflectively: through `getWebhooks()` when the model provides it (e.g. a MicroProfile OpenAPI 4.0 implementation), then through the swagger model behind an empoa-swagger-core wrapper (the path taken by the CLI, Gradle and Maven plugins); a model exposing neither is considered to have no webhooks. Why: the specification makes webhooks a first alternative to paths and components, and the validator had no awareness of it (#93), so valid webhooks-only documents failed the structural check. Fixes #93 Signed-off-by: Inf1mum --- README.md | 3 + .../cli/ValidationInitiatorTest.java | 20 ++++++ cli/src/test/resources/webhooksOnly.yaml | 24 +++++++ lib/build.gradle | 1 + .../ErrorAggregator.java | 6 +- .../OpenApiSpecStyleValidator.java | 49 ++++++++++++- .../ErrorAggregatorTest.java | 11 +++ .../OpenApiSpecStyleValidatorTest.java | 72 ++++++++++++++++++- 8 files changed, 180 insertions(+), 6 deletions(-) create mode 100644 cli/src/test/resources/webhooksOnly.yaml 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..f49a5b0f 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,55 @@ 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); + 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..ca1fd087 100644 --- a/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java +++ b/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java @@ -259,10 +259,80 @@ 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(Map.of("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(Map.of()).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(Map.of("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() { From 4a4eb73e058ffb7375ea39308df503dd68a1be69 Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Sun, 15 Nov 2026 19:20:35 +0300 Subject: [PATCH 2/2] test: stay within the Java 8 target and guard a missing swagger model What: the tests now build the webhooks maps with Collections.singletonMap and Collections.emptyMap instead of Map.of, which the codebase cannot use: the build targets Java 8 and Map.of is Java 9+. The reflective read of the swagger model behind an empoa wrapper also handles a null return of getSw() by considering the document free of webhooks, as the method comment describes, instead of failing with a NullPointerException. Why: addresses two review notes on PR #816. Signed-off-by: Inf1mum --- .../openapistylevalidator/OpenApiSpecStyleValidator.java | 3 +++ .../OpenApiSpecStyleValidatorTest.java | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java b/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java index f49a5b0f..050e2eea 100644 --- a/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java +++ b/lib/src/main/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidator.java @@ -80,6 +80,9 @@ private void validateStructure() { 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) { diff --git a/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java b/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java index ca1fd087..ce52b817 100644 --- a/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java +++ b/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java @@ -285,7 +285,7 @@ public Map getWebhooks @Test void shouldNotReportMissingPathsOrComponentsWhenThereAreWebhooks() { - OpenAPI openAPI = openAPIWithWebhooksAccessor(Map.of("onEvent", OASFactory.createPathItem())) + OpenAPI openAPI = openAPIWithWebhooksAccessor(Collections.singletonMap("onEvent", OASFactory.createPathItem())) .paths(null) .components(null); @@ -302,7 +302,8 @@ void shouldNotReportMissingPathsOrComponentsWhenThereAreWebhooks() { */ @Test void shouldNotReportMissingPathsOrComponentsWhenWebhooksMapIsEmpty() { - OpenAPI openAPI = openAPIWithWebhooksAccessor(Map.of()).paths(null).components(null); + OpenAPI openAPI = + openAPIWithWebhooksAccessor(Collections.emptyMap()).paths(null).components(null); OpenApiSpecStyleValidator validator = new OpenApiSpecStyleValidator(openAPI); @@ -319,7 +320,7 @@ void shouldNotReportMissingPathsOrComponentsWhenWebhooksMapIsEmpty() { @Test void shouldNotReportMissingPathsOrComponentsWhenWebhooksAreBehindSwaggerModel() { io.swagger.v3.oas.models.OpenAPI swaggerOpenAPI = new io.swagger.v3.oas.models.OpenAPI(); - swaggerOpenAPI.webhooks(Map.of("onEvent", new io.swagger.v3.oas.models.PathItem())); + 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() {