Skip to content

Commit

Permalink
GH-230 Cache processed types in properties to avoid infinite recursio…
Browse files Browse the repository at this point in the history
…n when going through createEmbeddedTypeDescription (Fix #230)
  • Loading branch information
dzikoysk committed Sep 4, 2024
1 parent 3d15957 commit 7e1ec6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ public static String getStatic() {
}

// by default nullable fields are not required, but we can force it
@OpenApiRequired
public String getNullableIsRequired() {
return "required";
//@OpenApiRequired
public JsonSchemaEntity getNullableIsRequired() {
return null;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ data class ResultScheme(

class TypeSchemaGenerator(val context: AnnotationProcessorContext) {

// The cache helps to avoid processing the same property multiple times & prevent infinite recursion
// ~ https://github.com/javalin/javalin-openapi/issues/230
private val processedProperties = mutableMapOf<Property, ResultScheme>()

fun createTypeSchema(
type: ClassDefinition,
inlineRefs: Boolean = false,
Expand Down Expand Up @@ -102,15 +106,23 @@ class TypeSchemaGenerator(val context: AnnotationProcessorContext) {
val properties = type.findAllProperties(requireNonNulls)

properties.forEach { property ->
val (propertySchema, refs) = createEmbeddedTypeDescription(
type = property.type,
inlineRefs = inlineRefs,
requiresNonNulls = requireNonNulls,
composition = property.composition,
extra = property.extra
)
propertiesObject.add(property.name, propertySchema)
references.addAll(refs)
val result =
when {
processedProperties.contains(property) ->
processedProperties[property]!!
else ->
createEmbeddedTypeDescription(
type = property.type,
inlineRefs = inlineRefs,
requiresNonNulls = requireNonNulls,
composition = property.composition,
extra = property.extra
).also {
processedProperties[property] = it
}
}
propertiesObject.add(property.name, result.json)
references.addAll(result.references)
}

if (properties.any { it.required }) {
Expand Down

0 comments on commit 7e1ec6d

Please sign in to comment.