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

Properties-driven path throws IllegalArgumentException #31671

Closed
piotrooo opened this issue Nov 24, 2023 · 4 comments
Closed

Properties-driven path throws IllegalArgumentException #31671

piotrooo opened this issue Nov 24, 2023 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@piotrooo
Copy link

piotrooo commented Nov 24, 2023

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 24, 2023
@sbrannen sbrannen added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 24, 2023
@sbrannen
Copy link
Member

sbrannen commented Nov 24, 2023

Since Spring Framework 6.1 / Spring Boot 3.2, you must compile your code with -parameters.

See the second paragraph of this section of the upgrade notes for details.

In addition, you should have seen a warning in the logs stating this with 6.0.x.

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2023
@snicoll
Copy link
Member

snicoll commented Nov 24, 2023

@piotrooo going forward, please follow-up on replies on previous issues you've created before creating a new one, see #31666.

@piotrooo
Copy link
Author

Sure, no problem, but I thouth that was a different case. Sorry.

@snicoll
Copy link
Member

snicoll commented Nov 24, 2023

I am sure you did but applying the fix for the first problem first is what we're asking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants