Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-232 Allow reference to a model from OpenApiContentProperty #232

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading