Skip to content

Commit

Permalink
allow reference to a model from OpenApiContentProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
f1qwase committed Sep 30, 2024
1 parent cff4deb commit 3e0056d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 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 @@ -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 3e0056d

Please sign in to comment.