Skip to content

Commit

Permalink
Vert.x clean up, move non-general methods from test base class (#2121)
Browse files Browse the repository at this point in the history
* Vert.x clean up, move non-general methods from test base class

Signed-off-by: Michael Edgar <[email protected]>

* refactor: additional Vert.x clean up and increase scenario coverage

Signed-off-by: Michael Edgar <[email protected]>

---------

Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar authored Jan 6, 2025
1 parent 849a5b9 commit 0409fed
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.smallrye.openapi.runtime.scanner;

import static io.smallrye.openapi.api.constants.OpenApiConstants.DUPLICATE_OPERATION_ID_BEHAVIOR;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -21,23 +19,18 @@
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.Operation;
import org.eclipse.microprofile.openapi.models.PathItem;
import org.eclipse.microprofile.openapi.models.media.Schema;
import org.eclipse.microprofile.openapi.models.parameters.Parameter;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
import org.json.JSONException;
import org.junit.jupiter.api.Assertions;
import org.skyscreamer.jsonassert.JSONAssert;

import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.api.SmallRyeOpenAPI;
import io.smallrye.openapi.model.Extensions;

public class IndexScannerTestBase {

Expand Down Expand Up @@ -133,40 +126,6 @@ public static String toJSON(OpenAPI oai) {
.toJSON();
}

public static void verifyMethodAndParamRefsPresent(OpenAPI oai) {
if (oai.getPaths() != null && oai.getPaths().getPathItems() != null) {
for (Map.Entry<String, PathItem> pathItemEntry : oai.getPaths().getPathItems().entrySet()) {
final PathItem pathItem = pathItemEntry.getValue();
if (pathItem.getOperations() != null) {
for (Map.Entry<PathItem.HttpMethod, Operation> operationEntry : pathItem.getOperations().entrySet()) {
final Operation operation = operationEntry.getValue();
String opRef = operationEntry.getKey() + " " + pathItemEntry.getKey();
Assertions.assertNotNull(Extensions.getMethodRef(operation), "methodRef: " + opRef);
if (operation.getParameters() != null) {
for (Parameter parameter : operation.getParameters()) {
/*
* if @Parameter style=matrix was not specified at the same @Path segment
* a synthetic parameter is created which cannot be mapped to a field or method parameter
*/
if (!isPathMatrixObject(parameter)) {
// in all other cases paramRef should be set
String pRef = opRef + ", " + parameter.getIn() + ": " + parameter.getName();
Assertions.assertNotNull(Extensions.getParamRef(parameter), "paramRef: " + pRef);
}
}
}
}
}
}
}
}

private static boolean isPathMatrixObject(Parameter parameter) {
return parameter.getIn() == Parameter.In.PATH && parameter.getStyle() == Parameter.Style.MATRIX
&& parameter.getSchema() != null && parameter.getSchema().getType() != null
&& parameter.getSchema().getType().equals(Collections.singletonList(Schema.SchemaType.OBJECT));
}

public static String schemaToString(String entityName, Schema schema) {
return toJSON(OASFactory.createOpenAPI()
.components(OASFactory.createComponents()
Expand Down Expand Up @@ -254,10 +213,6 @@ public static OpenApiConfig dynamicConfig(String key, Object value) {
return dynamicConfig(config);
}

public static OpenApiConfig failOnDuplicateOperationIdsConfig() {
return dynamicConfig(DUPLICATE_OPERATION_ID_BEHAVIOR, OpenApiConfig.DuplicateOperationIdBehavior.FAIL.name());
}

public static OpenApiConfig dynamicConfig(Map<String, String> properties) {
return OpenApiConfig.fromConfig(config(properties));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.smallrye.openapi.runtime.scanner;

import static io.smallrye.openapi.api.constants.OpenApiConstants.DUPLICATE_OPERATION_ID_BEHAVIOR;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.ByteArrayInputStream;
Expand All @@ -11,7 +12,9 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalLong;
Expand All @@ -24,6 +27,7 @@
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.PathItem;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexReader;
import org.jboss.jandex.IndexWriter;
Expand All @@ -32,6 +36,7 @@
import org.junit.jupiter.api.Test;

import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.model.Extensions;
import test.io.smallrye.openapi.runtime.scanner.Widget;
import test.io.smallrye.openapi.runtime.scanner.jakarta.MultipleContentTypesWithFormParamsTestResource;

Expand All @@ -40,6 +45,42 @@
*/
class ParameterScanTests extends IndexScannerTestBase {

public static void verifyMethodAndParamRefsPresent(OpenAPI oai) {
if (oai.getPaths() != null && oai.getPaths().getPathItems() != null) {
for (Map.Entry<String, PathItem> pathItemEntry : oai.getPaths().getPathItems().entrySet()) {
final PathItem pathItem = pathItemEntry.getValue();
if (pathItem.getOperations() != null) {
for (var operationEntry : pathItem.getOperations().entrySet()) {
var operation = operationEntry.getValue();
String opRef = operationEntry.getKey() + " " + pathItemEntry.getKey();
Assertions.assertNotNull(Extensions.getMethodRef(operation), "methodRef: " + opRef);
if (operation.getParameters() != null) {
for (var parameter : operation.getParameters()) {
/*
* if @Parameter style=matrix was not specified at the same @Path segment
* a synthetic parameter is created which cannot be mapped to a field or method parameter
*/
if (!isPathMatrixObject(parameter)) {
// in all other cases paramRef should be set
String pRef = opRef + ", " + parameter.getIn() + ": " + parameter.getName();
Assertions.assertNotNull(Extensions.getParamRef(parameter), "paramRef: " + pRef);
}
}
}
}
}
}
}
}

private static boolean isPathMatrixObject(org.eclipse.microprofile.openapi.models.parameters.Parameter parameter) {
return parameter.getIn() == org.eclipse.microprofile.openapi.models.parameters.Parameter.In.PATH
&& parameter.getStyle() == org.eclipse.microprofile.openapi.models.parameters.Parameter.Style.MATRIX
&& parameter.getSchema() != null && parameter.getSchema().getType() != null
&& parameter.getSchema().getType().equals(
Collections.singletonList(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType.OBJECT));
}

private static void test(String expectedResource, Class<?>... classes) throws IOException, JSONException {
Index index = indexOf(classes);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
Expand Down Expand Up @@ -163,10 +204,12 @@ void testJakartaMultipleContentTypesWithFormParams() throws IOException, JSONExc

@Test
void testFailOnDuplicateOperationIds() {
final OpenApiConfig config = failOnDuplicateOperationIdsConfig();
final IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> test(config, "params.multiple-content-types-with-form-params.json",
MultipleContentTypesWithFormParamsTestResource.class, Widget.class));
() -> test(
dynamicConfig(DUPLICATE_OPERATION_ID_BEHAVIOR, OpenApiConfig.DuplicateOperationIdBehavior.FAIL.name()),
"params.multiple-content-types-with-form-params.json",
MultipleContentTypesWithFormParamsTestResource.class,
Widget.class));
assertStartsWith(exception.getMessage(), "SROAP07950: Duplicate operationId:", "Exception message");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -63,6 +64,7 @@ public boolean isDeleteMethod(final MethodInfo method) {
}

@Override
@SuppressWarnings("deprecation")
public boolean containsScannerAnnotations(List<AnnotationInstance> instances,
List<AnnotationScannerExtension> extensions) {
for (AnnotationInstance instance : instances) {
Expand Down Expand Up @@ -149,14 +151,11 @@ private OpenAPI processRouteClass(ClassInfo routeClass) {
context.getResolverStack().push(resolver);

// Get the @RouteBase info and save it for later
AnnotationInstance routeBaseAnnotation = context.annotations().getAnnotation(routeClass,
VertxConstants.ROUTE_BASE);

if (routeBaseAnnotation != null) {
this.currentAppPath = routeBaseAnnotation.value("path").asString(); // TODO: Check if there and check for :
} else {
this.currentAppPath = "/";
}
this.currentAppPath = context.annotations().getAnnotationValue(
routeClass,
Collections.singletonList(VertxConstants.ROUTE_BASE),
"path",
"/");

// Process @OpenAPIDefinition annotation
OpenAPI openApi = processDefinitionAnnotation(context, routeClass);
Expand Down Expand Up @@ -312,8 +311,7 @@ private ResourceParameters getResourceParameters(final ClassInfo resourceClass,
final MethodInfo method) {
Function<AnnotationInstance, Parameter> reader = t -> context.io().parameterIO().read(t);
return VertxParameterProcessor.process(context, currentAppPath, resourceClass,
method, reader,
context.getExtensions());
method, reader);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.smallrye.openapi.vertx;

import static java.lang.invoke.MethodHandles.lookup;

import org.jboss.logging.Logger;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;

@MessageLogger(projectCode = "SROAP", length = 5)
interface VertxLogging {
VertxLogging log = Logger.getMessageLogger(VertxLogging.class, VertxLogging.class.getPackage().getName());
VertxLogging log = Logger.getMessageLogger(lookup(), VertxLogging.class, VertxLogging.class.getPackage().getName());

@LogMessage(level = Logger.Level.WARN)
@Message(id = 14000, value = "Ignoring %s annotation that is not on a method")
Expand Down
Loading

0 comments on commit 0409fed

Please sign in to comment.