Skip to content

Commit c40c53e

Browse files
fix: AnnotationUtils cache misses due to reinstantiation of ModelConverterContext (#5114)
* add test * fix: AnnotationUtils processed types cache misses * minor changes * fix log message * fix: context dropped in getSchemaFromAnnotation 6-arg overload and add tests * fix: AnnotationUtils processed types cache misses * fix: context dropped in getSchemaFromAnnotation 6-arg overload and add tests * remove timing tests --------- Co-authored-by: Ewa Ostrowska <ewa.ostrowska@smartbear.com>
1 parent 935d6c4 commit c40c53e

5 files changed

Lines changed: 526 additions & 15 deletions

File tree

modules/swagger-core/src/main/java/io/swagger/v3/core/converter/ModelConverterContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Schema resolve(AnnotatedType type) {
8787
processedTypes.add(type);
8888
}
8989
if (LOGGER.isDebugEnabled()) {
90-
LOGGER.debug(String.format("resolve %s", type.getType()));
90+
LOGGER.debug(String.format("resolve %s from %s", type.getType(), System.identityHashCode(this)));
9191
}
9292
Iterator<ModelConverter> converters = this.getConverters();
9393
Schema resolved = null;

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
821821
property = reResolvedProperty.get();
822822
}
823823

824-
reResolvedProperty = resolveArraySchemaWithCycleGuard(ctxArraySchema, annotatedType, openapi31, property);
824+
reResolvedProperty = resolveArraySchemaWithCycleGuard(ctxArraySchema, annotatedType, openapi31, property, context);
825825
if (reResolvedProperty.isPresent()) {
826826
property = reResolvedProperty.get();
827827
}
@@ -3547,15 +3547,15 @@ private Optional<Schema> resolveArraySchemaWithCycleGuard(
35473547
io.swagger.v3.oas.annotations.media.ArraySchema ctxArraySchema,
35483548
AnnotatedType annotatedType,
35493549
boolean openapi31,
3550-
Schema<?> property) {
3550+
Schema<?> property, ModelConverterContext context) {
35513551
boolean processSchemaImplementation = !typesBeingResolved.contains(annotatedType);
35523552
Optional<Schema> reResolvedProperty;
35533553
if (processSchemaImplementation) {
35543554
typesBeingResolved.add(annotatedType);
35553555
}
35563556
try {
35573557
reResolvedProperty = AnnotationsUtils.getArraySchema(ctxArraySchema, annotatedType.getComponents(), null,
3558-
openapi31, property, processSchemaImplementation);
3558+
openapi31, property, processSchemaImplementation, context);
35593559
} finally {
35603560
if (processSchemaImplementation) {
35613561
typesBeingResolved.remove(annotatedType);

modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.medi
493493
}
494494

495495
public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema arraySchema, Components components, JsonView jsonViewAnnotation, boolean openapi31, Schema existingSchema, boolean processSchemaImplementation) {
496+
return getArraySchema(arraySchema, components, jsonViewAnnotation, openapi31, existingSchema, processSchemaImplementation, null);
497+
}
498+
499+
public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema arraySchema, Components components, JsonView jsonViewAnnotation, boolean openapi31, Schema existingSchema, boolean processSchemaImplementation, ModelConverterContext context) {
496500
if (arraySchema == null || !hasArrayAnnotation(arraySchema)) {
497501
if (existingSchema == null) {
498502
return Optional.empty();
@@ -525,8 +529,8 @@ public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.medi
525529
arraySchemaObject.setMinItems(arraySchema.minItems());
526530
}
527531

528-
getSchemaFromAnnotation(arraySchema.contains(), components, jsonViewAnnotation, openapi31).ifPresent(arraySchemaObject::setContains);
529-
getSchemaFromAnnotation(arraySchema.unevaluatedItems(), components, jsonViewAnnotation, openapi31).ifPresent(arraySchemaObject::setUnevaluatedItems);
532+
getSchemaFromAnnotation(arraySchema.contains(), components, jsonViewAnnotation, openapi31, null, context).ifPresent(arraySchemaObject::setContains);
533+
getSchemaFromAnnotation(arraySchema.unevaluatedItems(), components, jsonViewAnnotation, openapi31, null, context).ifPresent(arraySchemaObject::setUnevaluatedItems);
530534

531535
if (arraySchema.maxContains() > 0) {
532536
arraySchemaObject.setMaxContains(arraySchema.maxContains());
@@ -536,7 +540,7 @@ public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.medi
536540
}
537541
if (arraySchema.prefixItems().length > 0) {
538542
for (io.swagger.v3.oas.annotations.media.Schema prefixItem : arraySchema.prefixItems()) {
539-
getSchemaFromAnnotation(prefixItem, components, jsonViewAnnotation, openapi31).ifPresent(arraySchemaObject::addPrefixItem);
543+
getSchemaFromAnnotation(prefixItem, components, jsonViewAnnotation, openapi31, null, context).ifPresent(arraySchemaObject::addPrefixItem);
540544
}
541545
}
542546

@@ -550,10 +554,10 @@ public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.medi
550554

551555
if (arraySchema.schema() != null) {
552556
if (arraySchema.schema().implementation().equals(Void.class)) {
553-
getSchemaFromAnnotation(arraySchema.schema(), components, jsonViewAnnotation, openapi31, arraySchemaObject.getItems())
557+
getSchemaFromAnnotation(arraySchema.schema(), components, jsonViewAnnotation, openapi31, arraySchemaObject.getItems(), context)
554558
.ifPresent(arraySchemaObject::setItems);
555559
} else if (processSchemaImplementation) {
556-
getSchema(arraySchema.schema(), arraySchema, false, arraySchema.schema().implementation(), components, jsonViewAnnotation, openapi31)
560+
getSchema(arraySchema.schema(), arraySchema, false, arraySchema.schema().implementation(), components, jsonViewAnnotation, openapi31, context)
557561
.ifPresent(arraySchemaObject::setItems);
558562
}
559563
}
@@ -622,7 +626,7 @@ public static Optional<Schema> getSchemaFromAnnotation(
622626
boolean openapi31,
623627
Schema existingSchema,
624628
ModelConverterContext context) {
625-
return getSchemaFromAnnotation(schema, components, jsonViewAnnotation, openapi31, existingSchema, Schema.SchemaResolution.DEFAULT, null);
629+
return getSchemaFromAnnotation(schema, components, jsonViewAnnotation, openapi31, existingSchema, Schema.SchemaResolution.DEFAULT, context);
626630
}
627631

628632
public static Optional<Schema> getSchemaFromAnnotation(
@@ -1888,21 +1892,31 @@ public static Optional<? extends Schema> getSchema(io.swagger.v3.oas.annotations
18881892
return getSchema(schemaAnnotation, arrayAnnotation, isArray, schemaImplementation, components, jsonViewAnnotation, false);
18891893

18901894
}
1891-
18921895
public static Optional<? extends Schema> getSchema(io.swagger.v3.oas.annotations.media.Schema schemaAnnotation,
18931896
io.swagger.v3.oas.annotations.media.ArraySchema arrayAnnotation,
18941897
boolean isArray,
18951898
Class<?> schemaImplementation,
18961899
Components components,
18971900
JsonView jsonViewAnnotation,
18981901
boolean openapi31) {
1902+
return getSchema(schemaAnnotation, arrayAnnotation, isArray, schemaImplementation, components, jsonViewAnnotation, openapi31, null);
1903+
}
1904+
1905+
public static Optional<? extends Schema> getSchema(io.swagger.v3.oas.annotations.media.Schema schemaAnnotation,
1906+
io.swagger.v3.oas.annotations.media.ArraySchema arrayAnnotation,
1907+
boolean isArray,
1908+
Class<?> schemaImplementation,
1909+
Components components,
1910+
JsonView jsonViewAnnotation,
1911+
boolean openapi31,
1912+
ModelConverterContext context) {
18991913
if (schemaImplementation != Void.class) {
1900-
Schema schemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation, openapi31, schemaAnnotation, arrayAnnotation, null);
1914+
Schema schemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation, openapi31, schemaAnnotation, arrayAnnotation, context);
19011915
if (StringUtils.isNotBlank(schemaAnnotation.format())) {
19021916
schemaObject.setFormat(schemaAnnotation.format());
19031917
}
19041918
if (isArray) {
1905-
Optional<Schema> arraySchema = AnnotationsUtils.getArraySchema(arrayAnnotation, components, jsonViewAnnotation, openapi31, null);
1919+
Optional<Schema> arraySchema = AnnotationsUtils.getArraySchema(arrayAnnotation, components, jsonViewAnnotation, openapi31, null, false, context);
19061920
if (arraySchema.isPresent()) {
19071921
arraySchema.get().setItems(schemaObject);
19081922
return arraySchema;
@@ -1914,15 +1928,15 @@ public static Optional<? extends Schema> getSchema(io.swagger.v3.oas.annotations
19141928
}
19151929

19161930
} else {
1917-
Optional<Schema> schemaFromAnnotation = AnnotationsUtils.getSchemaFromAnnotation(schemaAnnotation, components, jsonViewAnnotation, openapi31);
1931+
Optional<Schema> schemaFromAnnotation = AnnotationsUtils.getSchemaFromAnnotation(schemaAnnotation, components, jsonViewAnnotation, openapi31, null, context);
19181932
if (schemaFromAnnotation.isPresent()) {
19191933
if (StringUtils.isBlank(schemaFromAnnotation.get().get$ref()) && StringUtils.isBlank(schemaFromAnnotation.get().getType()) && !(schemaFromAnnotation.get() instanceof ComposedSchema)) {
19201934
// default to string
19211935
schemaFromAnnotation.get().setType("string");
19221936
}
19231937
return Optional.of(schemaFromAnnotation.get());
19241938
} else {
1925-
Optional<Schema> arraySchemaFromAnnotation = AnnotationsUtils.getArraySchema(arrayAnnotation, components, jsonViewAnnotation, openapi31, null);
1939+
Optional<Schema> arraySchemaFromAnnotation = AnnotationsUtils.getArraySchema(arrayAnnotation, components, jsonViewAnnotation, openapi31, null, false, context);
19261940
if (arraySchemaFromAnnotation.isPresent()) {
19271941
if (arraySchemaFromAnnotation.get().getItems() != null && StringUtils.isBlank(arraySchemaFromAnnotation.get().getItems().get$ref()) && StringUtils.isBlank(arraySchemaFromAnnotation.get().getItems().getType())) {
19281942
// default to string

0 commit comments

Comments
 (0)