ePages-de / restdocs-api-spec

Adds API specification support to Spring REST Docs
MIT License
391 stars 103 forks source link

Overlapping operationId problem #276

Open seheon-cho opened 2 days ago

seheon-cho commented 2 days ago

The test code and the created openapi3.yaml.

It's openapi3.yaml based on the above, and the problem I'm facing is that the first factor of the document() method of the class com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper comes from the combination of the identifier variable of the type String class in operationId.

I wrote the test through the MockMvc and Mockito libraries.

@DisplayName("fail 1")
@Test
void login_fail1() throws Exception {
    //given
    LoginRequest loginRequest = new LoginRequest(null, "aos", "loginId", "password", "82");

    //when
    when(authService.login(any()))
            .thenThrow(new BusinessException(AuthErrorCode.INVALID_DEVICE_TOKEN));

    //then
    mockMvc.perform(post("/auth/login")
                    .contentType("application/json")
                    .content(customObjectMapper.writeValueAsString(loginRequest))
            )
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.success").value(false))
            .andExpect(jsonPath("$.message").value("Invalid device token."))
            .andExpect(jsonPath("$.code").value(3003))
            .andExpect(jsonPath("$.errorData").value(Matchers.nullValue()))
            .andDo(document(
                    "Hello world!",
                    getDocumentRequest(),
                    getDocumentResponse(),
                    requestFields(
                            fieldWithPath("deviceToken").type(JsonFieldType.NULL).description("device token"),
                            fieldWithPath("deviceType").type(JsonFieldType.STRING).description("device type"),
                            fieldWithPath("email").type(JsonFieldType.STRING).description("email"),
                            fieldWithPath("password").type(JsonFieldType.STRING).description("pw"),
                            fieldWithPath("region").type(JsonFieldType.STRING).description("region")
                    ),
                    responseFields(
                         fieldWithPath("success").type(JsonFieldType.NULL).description("success"),
                          fieldWithPath("message").type(JsonFieldType.STRING).description("message"),
                          fieldWithPath("code").type(JsonFieldType.STRING).description("code"),
                          fieldWithPath("errorData").type(JsonFieldType.NULL).description("errorData")
                    )
            ));
}

@DisplayName("fail 2")
@Test
void login_fail_2() throws Exception {
    //given
    LoginRequest loginRequest = new LoginRequest("deviceToken", "aos", "loginId", "password", "82");

    //when
    when(authService.login(any()))
            .thenThrow(new BusinessException(AuthErrorCode.ALREADY_CONVERTED_USER));

    //then
    mockMvc.perform(post("/auth/login")
                    .contentType("application/json")
                    .content(customObjectMapper.writeValueAsString(loginRequest))
            )
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.success").value(false))
            .andExpect(jsonPath("$.message").value("test user."))
            .andExpect(jsonPath("$.code").value(3017))
            .andExpect(jsonPath("$.errorData").value(Matchers.nullValue()))
            .andDo(document(
                    "Nice to meet you!",
                    getDocumentRequest(),
                    getDocumentResponse(),
                    requestFields(
                            fieldWithPath("deviceToken").type(JsonFieldType.NULL).description("device token"),
                            fieldWithPath("deviceType").type(JsonFieldType.STRING).description("device type"),
                            fieldWithPath("email").type(JsonFieldType.STRING).description("email"),
                            fieldWithPath("password").type(JsonFieldType.STRING).description("pw"),
                            fieldWithPath("region").type(JsonFieldType.NULL).description("region")
                    ),
                    responseFields(
                         fieldWithPath("success").type(JsonFieldType.NULL).description("success"),
                          fieldWithPath("message").type(JsonFieldType.STRING).description("message"),
                          fieldWithPath("code").type(JsonFieldType.STRING).description("code"),
                          fieldWithPath("errorData").type(JsonFieldType.NULL).description("errorData")
                    )
            ));
}
openapi: 3.0.1
info:
  title: test API
  description: test API
  version: 0.1.0
servers:
- url: https://localhost:8080
tags: []
paths:
  /auth/login:
    post:
      tags:
      - auth
      operationId: Hello World!Nice to meet you!
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/auth-login2050674939'
            examples:
              Hello World!:
                value: |-
                  {
                    "deviceToken" : "deviceToken",
                    "deviceType" : "aos",
                    "email" : "loginId",
                    "password" : "password",
                    "region" : "82"
                  }
              Nice to meet you!:
                value: |-
                  {
                    "deviceToken" : "deviceToken",
                    "deviceType" : "aos",
                    "email" : "loginId",
                    "password" : "password",
                    "region" : "82"
                  }
      responses:
        "200":
          description: "200"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/auth-login1551218249'
              examples:
                Hello World!:
                  value: |-
                    {
                      "success" : false,
                      "message" : "Invalid device token.",
                      "code" : 3003,
                      "errorData" : null
                    }
                Nice to meet you!:
                  value: |-
                    {
                      "success" : false,
                      "message" : "test user.",
                      "code" : 3017,
                      "errorData" : null
                    }