Skip to content

Commit e3f2ea8

Browse files
xerialclaude
andauthored
fix: Refactor NettyServerConfig to use MessageCodecFactory directly (#4134)
## Summary - Change `NettyServerConfig` to store `MessageCodecFactory` instead of `PartialFunction` - Add `codecFactory` getter that ensures `withMapOutput` is applied for JSON serialization - Update `NettyResponseHandler` to accept `MessageCodecFactory` directly - Centralize codec factory creation to avoid duplication between response handler and request dispatcher This follows the suggestion from Gemini code review on #4132 to pass `MessageCodecFactory` directly instead of `PartialFunction`, improving maintainability by making the codec dependency more explicit. ## Test plan - [x] Compile netty module - [ ] CI tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 58b28c9 commit e3f2ea8

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

airframe-http-netty/src/main/scala/wvlet/airframe/http/netty/NettyResponseHandler.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ import wvlet.airframe.surface.{Primitive, Surface}
2222
import wvlet.airframe.rx.Rx
2323
import wvlet.log.LogSupport
2424

25-
class NettyResponseHandler(customCodec: PartialFunction[Surface, MessageCodec[_]] = PartialFunction.empty)
26-
extends ResponseHandler[Request, Response]
25+
class NettyResponseHandler(
26+
codecFactory: MessageCodecFactory = MessageCodecFactory.defaultFactoryForJSON
27+
) extends ResponseHandler[Request, Response]
2728
with LogSupport {
28-
private val codecFactory = {
29-
MessageCodecFactory.defaultFactoryForJSON
30-
.withCodecs(customCodec)
31-
}
3229

3330
override def toHttpResponse[A](route: Route, request: Request, responseSurface: Surface, a: A): Response = {
3431
a match {

airframe-http-netty/src/main/scala/wvlet/airframe/http/netty/NettyServer.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ case class NettyServerConfig(
5252
httpLoggerProvider: HttpLoggerConfig => HttpLogger = { (config: HttpLoggerConfig) =>
5353
new LogRotationHttpLogger(config)
5454
},
55-
customCodec: PartialFunction[Surface, MessageCodec[_]] = PartialFunction.empty,
55+
private val customCodecFactory: MessageCodecFactory = MessageCodecFactory.defaultFactory,
5656
// Thread manager for handling Future[_] responses
5757
executionContext: ExecutionContext = {
5858
// Using the global thread pool causes an issue in sbt's layered class loader #918
@@ -64,6 +64,11 @@ case class NettyServerConfig(
6464
) {
6565
lazy val port = serverPort.getOrElse(IOUtil.unusedPort)
6666

67+
/**
68+
* Get the codec factory with map output enabled for JSON serialization
69+
*/
70+
def codecFactory: MessageCodecFactory = customCodecFactory.withMapOutput
71+
6772
private[netty] def canUseEpoll: Boolean = {
6873
useEpoll && Epoll.isAvailable
6974
}
@@ -92,13 +97,16 @@ case class NettyServerConfig(
9297
)
9398
}
9499

100+
def withCodecFactory(factory: MessageCodecFactory): NettyServerConfig = {
101+
this.copy(customCodecFactory = factory)
102+
}
103+
95104
def withCustomCodec(p: PartialFunction[Surface, MessageCodec[_]]): NettyServerConfig = {
96-
this.copy(customCodec = p)
105+
this.copy(customCodecFactory = customCodecFactory.withCodecs(p))
97106
}
107+
98108
def withCustomCodec(m: Map[Surface, MessageCodec[_]]): NettyServerConfig = {
99-
this.copy(customCodec = customCodec.orElse {
100-
case s: Surface if m.contains(s) => m(s)
101-
})
109+
this.copy(customCodecFactory = customCodecFactory.withCodecs(m))
102110
}
103111

104112
def newServer(session: Session): NettyServer = {
@@ -140,7 +148,7 @@ case class NettyServerConfig(
140148
def newHttpLogger: HttpLogger = {
141149
val config = httpLoggerConfig
142150
.withExtraEntries(() => ListMap("server_name" -> name))
143-
.withCustomCodec(customCodec)
151+
.withCodecFactory(codecFactory)
144152
httpLoggerProvider(config)
145153
}
146154
}
@@ -235,9 +243,8 @@ class NettyServer(config: NettyServerConfig, session: Session) extends HttpServe
235243
config.router,
236244
config.controllerProvider,
237245
NettyBackend,
238-
new NettyResponseHandler(config.customCodec),
239-
// Set a custom codec and use JSON map output
240-
MessageCodecFactory.defaultFactoryForJSON.withCodecs(config.customCodec),
246+
new NettyResponseHandler(config.codecFactory),
247+
config.codecFactory,
241248
config.executionContext
242249
)
243250
)

0 commit comments

Comments
 (0)