Skip to content

Commit 62caf3c

Browse files
committed
Fix for Request Body
Signed-off-by: Shubham Kalloli <[email protected]>
1 parent 725aae2 commit 62caf3c

File tree

3 files changed

+15
-56
lines changed

3 files changed

+15
-56
lines changed

modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/api/GenApi.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ default ResponseEntity<ResponseCode> generateClient(
8686
summary = "Client generation with additional options",
8787
value = "{\"openAPIUrl\": \"https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml\", \"options\": {\"packageName\": \"com.example.petstore\", \"clientPackage\": \"com.example.petstore.client\", \"groupId\": \"com.example\", \"artifactId\": \"petstore-client\", \"artifactVersion\": \"1.0.0\"}}")
8888
}))
89-
@Valid GeneratorInput generatorInput) {
89+
@Valid
90+
@org.springframework.web.bind.annotation.RequestBody GeneratorInput generatorInput) {
9091
return getDelegate().generateClient(language, generatorInput);
9192
}
9293

@@ -109,7 +110,9 @@ default ResponseEntity<ResponseCode> generateServerForLanguage(
109110
summary = "Spring Boot server generation with options",
110111
value = "{\"openAPIUrl\": \"https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml\", \"options\": {\"packageName\": \"com.example.petstore.server\", \"basePackage\": \"com.example.petstore\", \"groupId\": \"com.example\", \"artifactId\": \"petstore-server\", \"artifactVersion\": \"1.0.0\"}}")
111112
}))
112-
@Valid GeneratorInput generatorInput) {
113+
@Valid
114+
@org.springframework.web.bind.annotation.RequestBody
115+
GeneratorInput generatorInput) {
113116
return getDelegate().generateServerForLanguage(framework, generatorInput);
114117
}
115118

modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/model/GeneratorInput.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,24 @@
2727
import java.util.List;
2828
import java.util.Map;
2929

30+
@Getter
3031
@Setter
3132
@Schema(description = "Configuration for building the client library")
3233
public class GeneratorInput {
3334

34-
@Getter
3535
@Schema(description = "OpenAPI specification as JSON object", example = "{\"openapi\": \"3.0.0\", \"info\": {\"title\": \"Sample API\", \"version\": \"1.0.0\"}}")
3636
private JsonNode spec;
3737

38-
@Getter
3938
@Schema(description = "Generator-specific options", example = "{\"packageName\": \"com.example.client\", \"clientPackage\": \"com.example.client\"}")
4039
private Map<String, String> options;
4140

4241
@Schema(description = "URL to the OpenAPI specification", example = "https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml", required = false)
4342
@JsonProperty("openAPIUrl")
4443
private String openAPIUrl;
4544

46-
@Getter
4745
@Schema(description = "Authorization value for accessing the OpenAPI specification")
4846
private AuthorizationValue authorizationValue;
4947

50-
@Getter
5148
@Schema(description = "OpenAPI normalizer rules", example = "[\"FILTER=operationId:updatePet\"]")
5249
private List<String> openapiNormalizer;
53-
54-
public String getOpenAPIUrl() {
55-
return openAPIUrl;
56-
}
5750
}

modules/openapi-generator-online/src/test/java/org/openapitools/codegen/online/api/GenApiControllerTest.java

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,20 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.junit.jupiter.api.Test;
55
import org.junit.jupiter.api.extension.ExtendWith;
6-
import org.openapitools.codegen.CliOption;
76
import org.openapitools.codegen.online.model.ResponseCode;
8-
import org.springframework.core.io.ByteArrayResource;
9-
import org.springframework.http.ResponseEntity;
107
import org.springframework.beans.factory.annotation.Autowired;
118
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
129
import org.springframework.http.HttpHeaders;
1310
import org.springframework.http.MediaType;
14-
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1511
import org.springframework.test.context.junit.jupiter.SpringExtension;
1612
import org.springframework.test.web.servlet.MockMvc;
13+
14+
import static org.hamcrest.Matchers.*;
1715
import static org.junit.jupiter.api.Assertions.assertTrue;
18-
import java.util.Arrays;
19-
import java.util.HashMap;
20-
import java.util.Map;
21-
22-
import static org.hamcrest.Matchers.hasItem;
23-
import static org.hamcrest.Matchers.not;
24-
import static org.mockito.ArgumentMatchers.any;
25-
import static org.mockito.ArgumentMatchers.anyString;
26-
import static org.mockito.Mockito.when;
16+
2717
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2818
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
2920
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
3021

3122
@ExtendWith(SpringExtension.class)
@@ -38,18 +29,13 @@ public class GenApiControllerTest {
3829
@Autowired
3930
private MockMvc mockMvc;
4031

41-
@MockitoBean
42-
private GenApiDelegate genApiDelegate;
43-
4432
@Test
4533
public void clientLanguages() throws Exception {
46-
when(genApiDelegate.clientOptions()).thenReturn(ResponseEntity.ok(Arrays.asList("java", "python", "javascript")));
4734
getLanguages("clients", "java");
4835
}
4936

5037
@Test
5138
public void serverFrameworks() throws Exception {
52-
when(genApiDelegate.serverOptions()).thenReturn(ResponseEntity.ok(Arrays.asList("spring", "nodejs", "flask")));
5339
getLanguages("servers", "spring");
5440
}
5541

@@ -63,32 +49,22 @@ public void getLanguages(String type, String expected) throws Exception {
6349

6450
@Test
6551
public void clientOptions() throws Exception {
66-
Map<String, CliOption> options = new HashMap<>();
67-
CliOption option = new CliOption("sortParamsByRequiredFlag", "Sort parameters by required flag");
68-
options.put("sortParamsByRequiredFlag", option);
69-
when(genApiDelegate.getClientOptions("java")).thenReturn(ResponseEntity.ok(options));
7052
getOptions("clients", "java");
7153
}
7254

7355
@Test
7456
public void clientOptionsUnknown() throws Exception {
75-
when(genApiDelegate.getClientOptions("unknown")).thenReturn(ResponseEntity.notFound().build());
7657
mockMvc.perform(get("/api/gen/clients/unknown"))
7758
.andExpect(status().isNotFound());
7859
}
7960

8061
@Test
8162
public void serverOptions() throws Exception {
82-
Map<String, CliOption> options = new HashMap<>();
83-
CliOption option = new CliOption("sortParamsByRequiredFlag", "Sort parameters by required flag");
84-
options.put("sortParamsByRequiredFlag", option);
85-
when(genApiDelegate.getServerOptions("spring")).thenReturn(ResponseEntity.ok(options));
8663
getOptions("servers", "spring");
8764
}
8865

8966
@Test
9067
public void serverOptionsUnknown() throws Exception {
91-
when(genApiDelegate.getServerOptions("unknown")).thenReturn(ResponseEntity.notFound().build());
9268
mockMvc.perform(get("/api/gen/servers/unknown"))
9369
.andExpect(status().isNotFound());
9470
}
@@ -102,26 +78,23 @@ private void getOptions(String type, String name) throws Exception {
10278

10379
@Test
10480
public void generateClient() throws Exception {
105-
when(genApiDelegate.generateClient(anyString(), any())).thenReturn(ResponseEntity.ok(new ResponseCode("test-code", "http://test.com:1234/api/gen/download/test-code")));
106-
when(genApiDelegate.downloadFile("test-code")).thenReturn(ResponseEntity.ok().contentType(org.springframework.http.MediaType.valueOf("application/zip")).header("Content-Length", "1024").body(new ByteArrayResource(new byte[1024])));
10781
generateAndDownload("clients", "java");
10882
}
10983

11084
@Test
11185
public void generateServer() throws Exception {
112-
when(genApiDelegate.generateServerForLanguage(anyString(), any())).thenReturn(ResponseEntity.ok(new ResponseCode("test-code", "http://test.com:1234/api/gen/download/test-code")));
113-
when(genApiDelegate.downloadFile("test-code")).thenReturn(ResponseEntity.ok().contentType(org.springframework.http.MediaType.valueOf("application/zip")).header("Content-Length", "1024").body(new ByteArrayResource(new byte[1024])));
11486
generateAndDownload("servers", "spring");
11587
}
11688

11789
private void generateAndDownload(String type, String name) throws Exception {
11890
String result = mockMvc.perform(post("http://test.com:1234/api/gen/" + type + "/" + name)
11991
.contentType(MediaType.APPLICATION_JSON)
12092
.content("{\"openAPIUrl\": \"" + OPENAPI_URL + "\"}"))
93+
.andDo(print())
12194
.andExpect(status().isOk())
12295
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
123-
.andExpect(jsonPath("$.code").value("test-code"))
124-
.andExpect(jsonPath("$.link").value("http://test.com:1234/api/gen/download/test-code"))
96+
.andExpect(jsonPath("$.code").value(matchesPattern(UUID_REGEX)))
97+
.andExpect(jsonPath("$.link").value(matchesPattern("http://test.com:1234/api/gen/download/" + UUID_REGEX)))
12598
.andReturn().getResponse().getContentAsString();
12699

127100
String code = new ObjectMapper().readValue(result, ResponseCode.class).getCode();
@@ -134,9 +107,6 @@ private void generateAndDownload(String type, String name) throws Exception {
134107

135108
@Test
136109
public void generateWIthForwardedHeaders() throws Exception {
137-
when(genApiDelegate.generateClient(anyString(), any())).thenReturn(ResponseEntity.ok(new ResponseCode("test-code", "https://forwarded.com:5678/api/gen/download/test-code")));
138-
when(genApiDelegate.downloadFile("test-code")).thenReturn(ResponseEntity.ok().contentType(org.springframework.http.MediaType.valueOf("application/zip")).header("Content-Length", "1024").body(new ByteArrayResource(new byte[1024])));
139-
140110
String result = mockMvc.perform(post("http://test.com:1234/api/gen/clients/java")
141111
.contentType(MediaType.APPLICATION_JSON)
142112
.header("X-Forwarded-Proto", "https")
@@ -145,8 +115,8 @@ public void generateWIthForwardedHeaders() throws Exception {
145115
.content("{\"openAPIUrl\": \"" + OPENAPI_URL + "\"}"))
146116
.andExpect(status().isOk())
147117
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
148-
.andExpect(jsonPath("$.code").value("test-code"))
149-
.andExpect(jsonPath("$.link").value("https://forwarded.com:5678/api/gen/download/test-code"))
118+
.andExpect(jsonPath("$.code").value(matchesPattern(UUID_REGEX)))
119+
.andExpect(jsonPath("$.link").value(matchesPattern("https://forwarded.com:5678/api/gen/download/" + UUID_REGEX)))
150120
.andReturn().getResponse().getContentAsString();
151121

152122
String code = new ObjectMapper().readValue(result, ResponseCode.class).getCode();
@@ -159,7 +129,6 @@ public void generateWIthForwardedHeaders() throws Exception {
159129

160130
@Test
161131
public void generateClientWithInvalidOpenAPIUrl() throws Exception {
162-
when(genApiDelegate.generateClient(anyString(), any())).thenReturn(ResponseEntity.badRequest().build());
163132
final String invalidOpenAPIUrl = "https://[::1]/invalid_openapi.json";
164133
mockMvc.perform(post("http://test.com:1234/api/gen/clients/java")
165134
.contentType(MediaType.APPLICATION_JSON)
@@ -169,12 +138,6 @@ public void generateClientWithInvalidOpenAPIUrl() throws Exception {
169138

170139
@Test
171140
public void generateWithOpenAPINormalizer() throws Exception {
172-
when(genApiDelegate.generateClient(anyString(), any()))
173-
.thenReturn(ResponseEntity.ok(new ResponseCode("test-code-1", "http://test.com:1234/api/gen/download/test-code-1")))
174-
.thenReturn(ResponseEntity.ok(new ResponseCode("test-code-2", "http://test.com:1234/api/gen/download/test-code-2")));
175-
when(genApiDelegate.downloadFile("test-code-1")).thenReturn(ResponseEntity.ok().contentType(org.springframework.http.MediaType.valueOf("application/zip")).header("Content-Length", "512").body(new ByteArrayResource(new byte[512])));
176-
when(genApiDelegate.downloadFile("test-code-2")).thenReturn(ResponseEntity.ok().contentType(org.springframework.http.MediaType.valueOf("application/zip")).header("Content-Length", "1024").body(new ByteArrayResource(new byte[1024])));
177-
178141
String withOpenAPINormalizer = "{\"openAPIUrl\":\"https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml\",\"openapiNormalizer\":[\"FILTER=operationId:updatePet\"],\"options\":{},\"spec\":{}}";
179142
String withoutOpenAPINormalizer = "{\"openAPIUrl\":\"https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml\",\"options\":{},\"spec\":{}}";
180143

0 commit comments

Comments
 (0)