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

bug with Form url encoded and a POJO with a list field #616

Closed
sdelamo opened this issue Oct 25, 2023 · 1 comment
Closed

bug with Form url encoded and a POJO with a list field #616

sdelamo opened this issue Oct 25, 2023 · 1 comment
Labels
type: bug Something isn't working

Comments

@sdelamo
Copy link
Contributor

sdelamo commented Oct 25, 2023

Issue description

With this record:

package com.example;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.serde.annotation.Serdeable;

import java.util.List;

@Serdeable
public record QuestionSave(String question,
                           List<Long> usersId) {
}

and this controller:

package com.example;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;

@Controller("/questions")
public class QuestionController {
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Post("/save")
    QuestionSave save(@Body QuestionSave questionSave) {
        return questionSave;
    }
}

and this test fails when the list contains one item:

package com.example;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest
class QuestionControllerTest {

    @Testpublic void formWithListOfOneItem(@Client("/") HttpClient client) throws IOException {
        String body = "question=en+que+trabajas&usersId=1";
        String expectedJson = "{\"question\":\"en que trabajas\",\"usersId\":[1]}";
        assertWithBody(client, body, expectedJson);
    }

    @Testpublic void formWithListOfMoreThanOne(@Client("/") HttpClient client) throws IOException {
        String body = "question=en+que+trabajas&usersId=1&usersId=2";
        String expectedJson = "{\"question\":\"en que trabajas\",\"usersId\":[1,2]}";
        assertWithBody(client, body, expectedJson);
    }

    private static void assertWithBody(HttpClient httpClient, String body, String expectedJson) throws IOException {
        BlockingHttpClient client = httpClient.toBlocking();
        String json = assertDoesNotThrow(() -> client.retrieve(HttpRequest.POST("/questions/save", body).contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE)));
        assertEquals(expectedJson, json);
    }
}

Both test pass when using Micronaut Jackson Databind.

@sdelamo
Copy link
Contributor Author

sdelamo commented Dec 4, 2023

this is now fixed in Micronaut Framework 4.2.0

@sdelamo sdelamo closed this as completed Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
No open projects
Status: Done
Development

No branches or pull requests

1 participant