Skip to content

Commit

Permalink
Minor cleanup, including fixing openapi validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterl committed Oct 3, 2023
1 parent fb7be59 commit b00966d
Show file tree
Hide file tree
Showing 10 changed files with 6,883 additions and 6,758 deletions.
8 changes: 3 additions & 5 deletions backend/src/main/kotlin/dev/dres/api/rest/RestApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ import dev.dres.api.rest.handler.evaluation.viewer.*
import dev.dres.api.rest.handler.judgement.*
import dev.dres.api.rest.handler.log.QueryLogHandler
import dev.dres.api.rest.handler.log.ResultLogHandler
import dev.dres.api.rest.handler.preview.GetExternalMediaHandler
import dev.dres.api.rest.handler.preview.*
import dev.dres.api.rest.handler.template.*
import dev.dres.api.rest.handler.preview.GetMediaHandler
import dev.dres.api.rest.handler.preview.PreviewImageHandler
import dev.dres.api.rest.handler.preview.PreviewVideoHandler
import dev.dres.api.rest.handler.scores.ListEvaluationScoreHandler
import dev.dres.api.rest.handler.submission.LegacySubmissionHandler
import dev.dres.api.rest.handler.submission.SubmissionHandler
Expand Down Expand Up @@ -104,7 +101,8 @@ object RestApi {
UserDetailsHandler(),

// Media
PreviewImageHandler(store, cache),
PreviewImageHandler(store, cache), /* [PreviewImageHandler] vs [PreviewImageTimelessHandler]: Optional path parameters are not allowed in OpenApi. [PreviewImageHandler] has timestamp as path parameter and must be initialised first */
PreviewImageTimelessHandler(store,cache), /* [PreviewImageHandler] vs [PreviewImageTimelessHandler]: Optional path parameters are not allowed in OpenApi */
PreviewVideoHandler(store, cache),
GetExternalMediaHandler(), // Must be registered before GetMediaHandler, as route is similar
GetMediaHandler(store),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import kotlinx.dnq.query.query
/**
* A general purpose, [AbstractPreviewHandler] that handles image previews for different [DbMediaItem].
*
* @author Ralph Gasser
* @version 1.0.0
* [PreviewImageHandler] vs [PreviewImageTimelessHandler]: Optional path parameters are not allowed in OpenApi.
*
* @author Ralph Gasser and Loris Sauter
* @version 1.1.0
*/
class PreviewImageHandler(store: TransientEntityStore, cache: CacheManager) : AbstractPreviewHandler(store, cache) {

Expand All @@ -25,7 +27,7 @@ class PreviewImageHandler(store: TransientEntityStore, cache: CacheManager) : Ab
operationId = OpenApiOperation.AUTO_GENERATE,
pathParams = [
OpenApiParam("mediaItemId", String::class, "Unique ID of the media item.", required = true, allowEmptyValue = false),
OpenApiParam("timestamp", Long::class, "Time into the video in milliseconds (for videos only).", required = false, allowEmptyValue = false)
OpenApiParam("timestamp", Long::class, "Time into the video in milliseconds (for videos only).", required = true, allowEmptyValue = false)
],
tags = ["Media"],
responses = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.dres.api.rest.handler.preview

import dev.dres.api.rest.types.status.ErrorStatusException
import dev.dres.data.model.media.DbMediaItem
import dev.dres.mgmt.cache.CacheManager
import io.javalin.http.Context
import io.javalin.openapi.*
import jetbrains.exodus.database.TransientEntityStore
import kotlinx.dnq.query.eq
import kotlinx.dnq.query.firstOrNull
import kotlinx.dnq.query.query

/**
* A general purpose, [AbstractPreviewHandler] that handles image previews for different [DbMediaItem].
*
* [PreviewImageHandler] vs [PreviewImageTimelessHandler]: Optional path parameters are not allowed in OpenApi.
*
* @author Ralph Gasser and Loris Sauter
* @version 1.1.0
*/
class PreviewImageTimelessHandler(store: TransientEntityStore, cache: CacheManager) : AbstractPreviewHandler(store, cache) {

override val route: String = "preview/{mediaItemId}"
@OpenApi(
summary = "Returns a preview image from a media item.",
path = "/api/v2/preview/{mediaItemId}",
operationId = OpenApiOperation.AUTO_GENERATE,
pathParams = [
OpenApiParam("mediaItemId", String::class, "Unique ID of the media item.", required = true, allowEmptyValue = false),
],
tags = ["Media"],
responses = [
OpenApiResponse("200", [OpenApiContent(type = "image/jpeg")]),
OpenApiResponse("202"),
OpenApiResponse("400"),
OpenApiResponse("404")
],
methods = [HttpMethod.GET]
)
override fun get(ctx: Context) {
val params = ctx.pathParamMap()
val mediaItemId = params["mediaItemId"] ?: throw ErrorStatusException(400, "Media item ID was not specified or is invalid.", ctx)
this.store.transactional(true) {
val item = DbMediaItem.query(DbMediaItem::id eq mediaItemId).firstOrNull()
handlePreviewImageRequest(item, 0L, ctx)
}
}

override fun doGet(ctx: Context): Any {
throw UnsupportedOperationException("PreviewImageHandler::doGet() is not supported and should not be executed!")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dres.api.rest.handler.template

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import dev.dres.DRES
import dev.dres.api.rest.handler.AccessManagedRestHandler
Expand Down Expand Up @@ -69,7 +70,7 @@ class ListTaskTypePresetsHandler : AccessManagedRestHandler, GetRestHandler<List

LOGGER.trace("Internal preset files (jar) resources: {}", list2.toString())

list2.map{ ObjectMapper().readValue(it, ApiTaskType::class.java) }
list2.map{ ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false).readValue(it, ApiTaskType::class.java) }
}catch(e: Exception){
LOGGER.error("Error upon loading internal presets.", e)
LOGGER.warn("An error occurred during loading of internal task type presets. Only external ones are available. Check logs accordingly.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import dev.dres.data.model.template.task.DbTaskGroup
* @author Ralph Gasser
* @version 1.0.0
*/
data class ApiTaskGroup(val name: String, val type: String)
data class ApiTaskGroup(val id: String?,val name: String, val type: String)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.dres.api.rest.types.template.tasks

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import dev.dres.api.rest.types.template.tasks.options.*
import dev.dres.data.model.template.task.DbTaskType
Expand All @@ -15,6 +17,7 @@ import java.nio.file.StandardOpenOption
* @version 1.1.0
*/
data class ApiTaskType(
val id: String?,
val name: String,
val duration: Long,
val targetOption: ApiTargetOption,
Expand All @@ -25,8 +28,8 @@ data class ApiTaskType(
val configuration: Map<String, String>
) {

constructor() : this("Default TaskType DO NOT USE!",
300,
constructor() : this("---NO_ID---","---Default TaskType DO NOT USE!---",
1,
ApiTargetOption.TEXT, listOf(ApiHintOption.TEXT),
listOf(ApiSubmissionOption.TEXTUAL_SUBMISSION),
listOf(ApiTaskOption.HIDDEN_RESULTS), ApiScoreOption.KIS, mapOf()
Expand All @@ -40,7 +43,7 @@ data class ApiTaskType(
*/
fun read(file: Path): ApiTaskType =
Files.newInputStream(file, StandardOpenOption.READ).use {
ObjectMapper().readValue(it, ApiTaskType::class.java)
ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(it, ApiTaskType::class.java)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class DbTaskGroup(entity: Entity) : XdEntity(entity) {
*
* @return [ApiTargetType]
*/
fun toApi(): ApiTaskGroup = ApiTaskGroup(this.name,this.type.name)
fun toApi(): ApiTaskGroup = ApiTaskGroup(this.xdId,this.name,this.type.name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class DbTaskType(entity: Entity) : XdEntity(entity) {
* @return [ApiTaskType]
*/
fun toApi(): ApiTaskType = ApiTaskType(
id=this.xdId,
name = this.name,
duration = this.duration,
targetOption = this.target.toApi(),
Expand Down
9 changes: 6 additions & 3 deletions doc/oas-client.json
Original file line number Diff line number Diff line change
Expand Up @@ -1607,9 +1607,6 @@
"text" : {
"type" : "string"
},
"mediaItemId" : {
"type" : "string"
},
"mediaItemName" : {
"type" : "string"
},
Expand Down Expand Up @@ -2090,6 +2087,9 @@
"type" : "object",
"additionalProperties" : false,
"properties" : {
"id" : {
"type" : "string"
},
"name" : {
"type" : "string"
},
Expand Down Expand Up @@ -2144,6 +2144,9 @@
"type" : "object",
"additionalProperties" : false,
"properties" : {
"id" : {
"type" : "string"
},
"name" : {
"type" : "string"
},
Expand Down
Loading

0 comments on commit b00966d

Please sign in to comment.