Skip to content

Properties-driven path throws IllegalArgumentException #31671

Closed as not planned
Closed as not planned
@piotrooo

Description

@piotrooo

Affects: 6.1.1


Following code works in 6.0.13.

Let's create a method where the path is set from properties. In the provided example, the path could look /api/sample-callback-path/{client}.

@PostMapping(path = "${some.path}", consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(NO_CONTENT)
public void receiveEvent(
        @SuppressWarnings("MVCPathVariableInspection") @PathVariable String client,
        @RequestBody @Valid EventTO event
) {
    requireNonNull(client, "client cannot be null");
    requireNonNull(event, "event cannot be null");
    checkClient(client, event);

    try {
        eventProcessor.process(event);
    } catch (RuntimeException e) {
        logger.error("Error while processing event {}.", event, e);
        throw e;
    }
}

I have a test that attempts to make a request:

@WebMvcTest(controllers = EventReceiverController.class)
@TestPropertySource(properties = "some.path=/api/sample-callback-path/{client}")
class EventReceiverControllerItTest {
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private EventProcessor eventProcessor;

    @Test
    void shouldReceiveAndProcessEvent() throws Exception {
        // given
        String client = "some-client";

        String json = """
                {
                    "type": "instanceCreated",
                    "version": 1,
                    "timestamp": "2023-11-08T07:52:58.000Z",
                    "client": "%s",
                    "instance": {
                        "environment": "stage",
                        "network": {}
                    }
                }
                """.formatted(client);

        // when
        ResultActions result = mockMvc
                .perform(post("/api/sample-callback-path/{client}", client)
                        .contentType(APPLICATION_JSON)
                        .content(json)
                );

        // then
        result.andExpect(status().isNoContent());

        verify(eventProcessor).process(any(EventTO.class));
        verifyNoMoreInteractions(eventProcessor);
    }

    @SpringBootApplication
    static class Application {
    }
}

I receive a following exception:

jakarta.servlet.ServletException: Request processing failed: java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not found in class file either.

	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1022)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914)
	at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:547)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
	at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
	at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614)
	at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:165)
	at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:132)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
	at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:132)
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
	at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:132)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
	at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:132)
	at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:201)

When I set @PathVariable("client") String client, everything works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions