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

Duplicate parts in generated snippets for multipart requests documented with MockMvc #746

Closed
BeanVortex opened this issue Aug 31, 2021 · 2 comments

Comments

@BeanVortex
Copy link

Hi.
I have this test method that documents multipart request:

    @Autowired
    private static final RestDocumentationContextProvider restDocumentation;
    private static MockMvc mockMvc;

    @BeforeEach
    void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .apply(documentationConfiguration(restDocumentation))
                .alwaysDo(document("{method-name}"))
                .build();
    }

    @Test
    @Order(1)
    @WithMockUser(username = "anonymousUser")
    void signUpUser() throws Exception {
        var file1 = new MockMultipartFile("profileFile", "hello.jpg", MediaType.IMAGE_JPEG_VALUE,
                "Hello, World!".getBytes());
        var file2 = new MockMultipartFile("shopFile", "hello.jpg", MediaType.IMAGE_JPEG_VALUE,
                "Hello, World!".getBytes());
        var address = new MockPart("address", "address".getBytes());
        var des = new MockPart("description", "desc".getBytes());
        var username = new MockPart("userName", "user n".getBytes());
        var password = new MockPart("password", "pass1".getBytes());
        var passwordRepeat = new MockPart("passwordRepeat", "pass1".getBytes());
        var email = new MockPart("email", "email@mail.com".getBytes());
        MockPart[] parts = {email, des, username, address, passwordRepeat, password};
        mockMvc.perform(multipart("/api/user/signup/")
                .file(file1)
                .file(file2)
                .part(parts)
                .accept(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andDo(result -> {
                    signupRefreshToken = result.getResponse().getHeader("refresh_token");
                    signupAccessToken = result.getResponse().getHeader("access_token");
                    JSONObject obj = new JSONObject(result.getResponse().getContentAsString());
                    userId = obj.getLong("id");
                    profileImage = obj.getString("profileImage");
                    shopImage = obj.getString("shopImage");

                })
                .andExpect(status().isOk())
                .andExpect(jsonPath("$").isMap())
                .andExpect(jsonPath("$.id").isNotEmpty())
        ;
    }
     

The generated curl document for this api is like this:

[source,bash]
----
$ curl 'http://localhost:8080/api/user/signup/' -i -X POST \
    -H 'Content-Type: multipart/form-data' \
    -H 'Accept: application/json' \
    -F 'email=email@mail.com' \
    -F 'description=desc' \
    -F 'userName=user n' \
    -F 'address=address' \
    -F 'passwordRepeat=pass1' \
    -F 'password=pass1' \
    -F 'profileFile=@hello.jpg;type=image/jpeg' \
    -F 'shopFile=@hello.jpg;type=image/jpeg' \
    -F 'email=email@mail.com' \
    -F 'description=desc' \
    -F 'userName=user n' \
    -F 'address=address' \
    -F 'passwordRepeat=pass1' \
    -F 'password=pass1'
----

so some of multipart fields are duplicated.
And also for http one:

[source,http,options="nowrap"]
----
POST /api/user/signup/ HTTP/1.1
Content-Type: multipart/form-data; boundary=6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Accept: application/json
Host: localhost:8080

--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=email

email@mail.com
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=description

desc
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=userName

user n
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=address

address
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=passwordRepeat

pass1
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=password

pass1
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=email

email@mail.com
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=description

desc
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=userName

user n
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=address

address
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=passwordRepeat

pass1
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=password

pass1
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=profileFile; filename=hello.jpg
Content-Type: image/jpeg

Hello, World!
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=shopFile; filename=hello.jpg
Content-Type: image/jpeg

Hello, World!
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm--
----
@wilkinsona
Copy link
Member

Thanks for the report. I've reproduced the behaviour and suspect that it's due to the changes made for spring-projects/spring-framework#26400. Can you confirm that you're using Spring Framework 5.3.4 or later? It'd also be useful to know the version of Spring REST Docs that you're using as well.

@BeanVortex
Copy link
Author

By running application:

   public static void main(String[] args) {
        SpringApplication.run(AnbarinooApplication.class, args);
        System.out.println(SpringVersion.getVersion());
    }

I get 5.3.7 for spring framework version.

restdocs version is 2.0.5.RELEASE:

    testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc:2.0.5.RELEASE'

@wilkinsona wilkinsona added this to the 2.0.6.RELEASE milestone Nov 18, 2021
@wilkinsona wilkinsona changed the title generates duplicate fields in multipart request Duplicate parts in generated snippets for multipart requests Jan 10, 2022
@wilkinsona wilkinsona changed the title Duplicate parts in generated snippets for multipart requests Duplicate parts in generated snippets for multipart requests documented with MockMvc Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants