Skip to content

Fix JAX-RS generator: missing MediaType import + illegal throws widening#101

Closed
Syvlain wants to merge 1 commit into
Unknow0:masterfrom
Syvlain:fix-jaxrs-mediatype-import-missing
Closed

Fix JAX-RS generator: missing MediaType import + illegal throws widening#101
Syvlain wants to merge 1 commit into
Unknow0:masterfrom
Syvlain:fix-jaxrs-mediatype-import-missing

Conversation

@Syvlain
Copy link
Copy Markdown

@Syvlain Syvlain commented May 28, 2026

Two regressions surfaced on mcp-ospita (single @produces, single mapping per HTTP method — minimal resource graph).

  1. Missing import in generated per-servlet CU MediaTypesBuilder builds FieldAccessExpr like MediaType.APPLICATION_JSON_TYPE with mt = types.getClass(MediaType.class) against the MediaTypes class CU. JaxRsServletBuilder.buildProduces then re-uses those Expressions in the per-servlet CU (line ~386, the second arg of req.getAccepted(...)) without registering MediaType on that CU, so the generated servlet references the simple name with no matching import. Compile fails with "cannot find symbol variable MediaType". In richer resource graphs the import is added incidentally by other code paths; minimal resources don't hit those. Fix: call types.getClass(MediaType.class) on the consumer CU before constructing the call.

  2. Illegal override-widening of throws on service(...) do and $call are emitted with addThrownException(Exception.class). service(...) overrides HttpServlet.service which is declared throws ServletException, IOException; an override cannot widen the throws set, so the build fails with "unreported exception java.lang.Exception". Fix (minimal): narrow do/$call to throws IOException, ServletException too. Works as long as user resource methods don't declare checked throws beyond those — a more robust fix would wrap user-method calls in $call with try/catch(Exception) -> ServletException, but that's a broader change.

Two regressions surfaced on mcp-ospita (single @produces, single mapping per
HTTP method, non-wildcard paths — i.e. the SimpleService dispatch path, which
the test fixtures never exercise since they only use a wildcard resource).

1. Illegal throws on service(...) in the SimpleService path
   do<Method>/$call are emitted with `throws Exception`. In PatternService that
   is fine because the dispatch goes through service$<id>, which wraps the call
   chain in try/catch(Throwable) -> JaxrsContext.sendError. SimpleService had no
   such wrapper: service(...) (override of HttpServlet.service, limited to
   `throws ServletException, IOException`) called do<Method> directly, so the
   `throws Exception` leaked out as "unreported exception java.lang.Exception".
   Fix: wrap SimpleService's dispatch in the same try/catch(Throwable) ->
   JaxrsContext.sendError that PatternService already uses. do<Method>/$call keep
   `throws Exception` (correct: $call invokes the user resource method directly,
   which may declare arbitrary checked exceptions), and both paths now route
   errors identically.

2. Missing MediaType import in generated per-servlet CU
   MediaTypesBuilder.type(..) returns either MediaType.XXX (well-known type) or
   MediaTypes.XXX (custom static field). JaxRsServletBuilder.buildProduces inlines
   that expression into the per-servlet CU. For well-known types the simple name
   MediaType is referenced without a matching import on minimal resources (richer
   graphs add it incidentally), so compile fails with "cannot find symbol".
   Fix: register MediaType on the consumer CU only when the inlined expression
   actually references MediaType (well-known), not for custom MediaTypes.XXX
   fields — avoiding a spurious unused import.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Syvlain Syvlain force-pushed the fix-jaxrs-mediatype-import-missing branch from aec3282 to bc49406 Compare May 29, 2026 10:21
@sonarqubecloud
Copy link
Copy Markdown

@Unknow0
Copy link
Copy Markdown
Owner

Unknow0 commented May 29, 2026

Hi,

done in #102 and #103

the change on the exception will break if the service class throw something we need to catch them
i also updated the test project to have better covering on the generation path

Regards.

@Unknow0 Unknow0 closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants