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

Add content-type header based on endpoint body #2046

Merged
merged 1 commit into from
Mar 16, 2023
Merged
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 @@ -128,14 +128,15 @@ private[codec] object EncoderDecoder {
final def encodeWith[Z](value: Value)(f: (URL, Option[Status], Option[Method], Headers, Body) => Z): Z = {
val inputs = deconstructor(value)

val path = encodePath(inputs.path)
val query = encodeQuery(inputs.query)
val status = encodeStatus(inputs.status)
val method = encodeMethod(inputs.method)
val headers = encodeHeaders(inputs.header)
val body = encodeBody(inputs.content)

f(URL(path, queryParams = query), status, method, headers, body)
val path = encodePath(inputs.path)
val query = encodeQuery(inputs.query)
val status = encodeStatus(inputs.status)
val method = encodeMethod(inputs.method)
val headers = encodeHeaders(inputs.header)
val body = encodeBody(inputs.content)
val contentTypeHeaders = encodeContentType(inputs.content)

f(URL(path, queryParams = query), status, method, headers ++ contentTypeHeaders, body)
}

private def decodePaths(path: Path, inputs: Array[Any]): Unit = {
Expand Down Expand Up @@ -294,7 +295,7 @@ private[codec] object EncoderDecoder {
}

private def encodeHeaders(inputs: Array[Any]): Headers = {
var headers = Headers.contentType("application/json")
var headers = Headers.empty

var i = 0
while (i < inputs.length) {
Expand Down Expand Up @@ -327,5 +328,12 @@ private[codec] object EncoderDecoder {
encoder(inputs(0))
} else throw new IllegalStateException("A request on a REST endpoint should have at most one body")
}

private def encodeContentType(inputs: Array[Any]): Headers = {
if (jsonEncoders.length == 0) Headers.empty
else if (jsonEncoders.length == 1) {
Headers.contentType(MediaType.application.json.fullType)
} else throw new IllegalStateException("A request on a REST endpoint should have at most one body")
}
}
}