Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,26 @@ void shouldNotFailWhenThereAreNoPaths() throws Throwable {

assertDoesNotThrow(() -> validationInitiator.validate(optionManager, commandLine));
}

/**
* See <a href="https://github.com/OpenAPITools/openapi-style-validator/issues/93">https://github.com/OpenAPITools/openapi-style-validator/issues/93</a>
*
* <p>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<StyleError> errorList = validationInitiator.validate(optionManager, commandLine);

assertEquals(0, errorList.size());
}
}

private void assertThrowsIllegalArgumentExceptionWithExceptedMessage(
Expand Down
24 changes: 24 additions & 0 deletions cli/src/test/resources/webhooksOnly.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,11 +42,58 @@ public List<StyleError> 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;
Comment thread
Inf1mum marked this conversation as resolved.
Resolved
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) {
Comment thread
Inf1mum marked this conversation as resolved.
Resolved
return null;
}

try {
Method getSw = openAPI.getClass().getMethod("getSw");
Object swaggerOpenAPI = getSw.invoke(openAPI);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, org.eclipse.microprofile.openapi.models.PathItem> webhooks) {
return new org.openapitools.empoa.simple.internal.models.OpenAPIImpl() {
@SuppressWarnings("unused")
public Map<String, org.eclipse.microprofile.openapi.models.PathItem> 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<StyleError> 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<StyleError> 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<StyleError> errors = validator.validate(new ValidatorParameters());
assertEquals(0, errors.size());
}

/* tests for naming convention parameters */
@Test
void shouldReportPathNamingConventionError() {
Expand Down