Skip to content

Commit

Permalink
Add reproducer test self-referencing (#1565) (#1636)
Browse files Browse the repository at this point in the history
* Reproduce issue #1565

Branch off 3.5.2 as that's what included in Quarkus 3.4.x where I
encountered the bug.

Later I realised that it was already fixed in 3.6.x (Quarkus 3.5.0)

* Rename and improve test for #1565

* Format code according to formatter configuration
  • Loading branch information
pe-st authored Nov 5, 2023
1 parent 0986946 commit 9e6f263
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.smallrye.openapi.runtime.scanner;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;

import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.models.media.Schema;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Type;
import org.jboss.jandex.Type.Kind;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

Expand Down Expand Up @@ -41,4 +45,42 @@ void testStreams(String type, Schema.SchemaType itemType, String itemFormat) thr
assertEquals(itemFormat, out.getItems().getFormat());
}

@Test
void testNoSelfReferencingRegardlessOfScanOrder() throws IOException {
// one way to trigger issue 1565 (self-referencing) was to have a
// non-SchemaType.OBJECT type as a field in another type
@org.eclipse.microprofile.openapi.annotations.media.Schema(description = "Nested class", type = SchemaType.STRING)
class N {
}

@org.eclipse.microprofile.openapi.annotations.media.Schema(description = "Containing class")
class C {
N n;
}

IndexView index = Index.of(C.class, N.class);
AnnotationScannerContext contextCFirst = new AnnotationScannerContext(index,
Thread.currentThread().getContextClassLoader(),
IndexScannerTestBase.emptyConfig());
AnnotationScannerContext contextNFirst = new AnnotationScannerContext(index,
Thread.currentThread().getContextClassLoader(),
IndexScannerTestBase.emptyConfig());
Type cType = Type.create(DotName.createSimple(C.class), Kind.CLASS);
Type nType = Type.create(DotName.createSimple(N.class), Kind.CLASS);

// io.smallrye.openapi.runtime.scanner.OpenApiAnnotationScanner.processClassSchemas()
// calls OpenApiDataObjectScanner.process() for all annotated classes in 'index',
// and the order of the calls might not be deterministic.
// Try both orders for the two example classes and expect the same result
OpenApiDataObjectScanner.process(contextCFirst, cType);
OpenApiDataObjectScanner.process(contextCFirst, nType);

OpenApiDataObjectScanner.process(contextNFirst, nType);
OpenApiDataObjectScanner.process(contextNFirst, cType);

// the contextCFirst case had "#/components/schemas/1N" from getRef() in smallrye-open-api 3.5.2
assertAll(
() -> assertNull(contextCFirst.getOpenApi().getComponents().getSchemas().get("1N").getRef(), "C first"),
() -> assertNull(contextNFirst.getOpenApi().getComponents().getSchemas().get("1N").getRef(), "N first"));
}
}

0 comments on commit 9e6f263

Please sign in to comment.