Skip to content

Commit

Permalink
GH-232 Allow reference to a model from OpenApiContentProperty (#232)
Browse files Browse the repository at this point in the history
* allow reference to a model from OpenApiContentProperty

* add test for recursive class
  • Loading branch information
f1qwase authored Oct 7, 2024
1 parent cff4deb commit 350ff9b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public static void main(String[] args) {
@OpenApiContent(mimeType = "image/png", type = "string", format = "base64"), // single file upload,
@OpenApiContent(mimeType = "multipart/form-data", properties = {
@OpenApiContentProperty(name = "form-element", type = "integer"), // random element in form-data
@OpenApiContentProperty(name = "reference", from = KotlinEntity.class), // reference to another object
@OpenApiContentProperty(name = "file-name", isArray = true, type = "string", format = "base64") // multi-file upload
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public static void main(String[] args) {
@OpenApiContent(mimeType = "image/png", type = "string", format = "base64"), // single file upload,
@OpenApiContent(mimeType = "multipart/form-data", properties = {
@OpenApiContentProperty(name = "form-element", type = "integer"), // random element in form-data
@OpenApiContentProperty(name = "reference", from = KotlinEntity.class) // reference to another object
@OpenApiContentProperty(name = "file-name", isArray = true, type = "string", format = "base64") // multi-file upload
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,22 +444,29 @@ internal class OpenApiGenerator {
schema.addProperty("type", "object")

for (contentProperty in properties) {
val propertyScheme = JsonObject()
val propertyFormat = contentProperty.format.takeIf { it != NULL_STRING }

if (contentProperty.isArray) {
propertyScheme.addProperty("type", "array")

val items = JsonObject()
items.addProperty("type", contentProperty.type)
propertyFormat?.let { items.addProperty("format", it) }
propertyScheme.add("items", items)
val contentPropertyFrom = contentAnnotation.getTypeMirror { contentProperty.from }
val propertyScheme = if (contentPropertyFrom.getFullName() != NULL_CLASS::class.java.name) {
createTypeDescriptionWithReferences(contentPropertyFrom)
} else {
propertyScheme.addProperty("type", contentProperty.type)
propertyFormat?.let { propertyScheme.addProperty("format", it) }
JsonObject().apply {
addProperty("type", contentProperty.type)
propertyFormat?.let { addProperty("format", it) }
}
}

propertiesSchema.add(contentProperty.name, propertyScheme)
propertiesSchema.add(contentProperty.name,
if (contentProperty.isArray) {
// wrap into OpenAPI array object
JsonObject().apply {
addProperty("type", "array")
add("items", propertyScheme)
}
} else {
propertyScheme
}
)
}

schema.add("properties", propertiesSchema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,21 @@ internal class TypeMappersTest : OpenApiAnnotationProcessorSpecification() {
))
}

}
private class Loop(
val self: Loop?,
)


@OpenApi(
path = "recursive",
versions = ["should_map_recursive_type"],
responses = [OpenApiResponse(status = "200", content = [OpenApiContent(from = Loop::class)])]
)
@Test
fun should_map_recursive_type() = withOpenApi("should_map_recursive_type") {
assertThatJson(it)
.inPath("$.components.schemas.Loop.properties.self")
.isObject
.containsEntry("\$ref", "#/components/schemas/Loop")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ annotation class OpenApiContent(
@Target()
@Retention(RUNTIME)
annotation class OpenApiContentProperty(
val from: KClass<*> = NULL_CLASS::class,
val name: String,
val isArray: Boolean = false,
val type: String,
val type: String = NULL_STRING,
val format: String = NULL_STRING
)

Expand Down
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from bffdf4 to 204546

0 comments on commit 350ff9b

Please sign in to comment.