Skip to content

Commit da3b3b6

Browse files
kakulisenchanjarster
authored andcommitted
[SCB-1673] fix the problem according code review suggestion
Signed-off-by: kakulisen <[email protected]>
1 parent 05972ce commit da3b3b6

File tree

4 files changed

+114
-34
lines changed

4 files changed

+114
-34
lines changed

oas-generator/oas-generator-core/src/main/java/org/apache/servicecomb/toolkit/generator/context/OasContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class OasContext implements IExtensionsContext {
4949

5050
private String httpMethod;
5151

52-
private String[] consumers;
52+
private String[] consumes;
5353

5454
private String[] produces;
5555

@@ -180,11 +180,11 @@ public void setHttpMethod(String httpMethod) {
180180
}
181181

182182
public String[] getConsumers() {
183-
return consumers;
183+
return consumes;
184184
}
185185

186-
public void setConsumers(String[] consumers) {
187-
this.consumers = consumers;
186+
public void setConsumers(String[] consumes) {
187+
this.consumes = consumes;
188188
}
189189

190190
public String[] getProduces() {

oas-generator/oas-generator-core/src/main/java/org/apache/servicecomb/toolkit/generator/context/OperationContext.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class OperationContext implements IExtensionsContext {
7676

7777
private List<String> tags;
7878

79-
private String[] consumers;
79+
private String[] consumes;
8080

8181
private String[] produces;
8282

@@ -131,7 +131,7 @@ public Operation toOperation() {
131131
mediaType.schema(schema);
132132
}
133133
schema.addProperties(parameterContext.getName(), parameterContext.getSchema());
134-
if (consumers != null) {
134+
if (consumes != null) {
135135
for (String consume : getConsumers()) {
136136
content.addMediaType(consume, mediaType);
137137
}
@@ -168,11 +168,7 @@ public Operation toOperation() {
168168

169169
private void processHeaders() {
170170

171-
if (headers == null) {
172-
headers = parentContext.getHeaders();
173-
}
174-
175-
if (headers == null) {
171+
if (getHeaders() == null) {
176172
return;
177173
}
178174

@@ -191,11 +187,7 @@ private void processHeaders() {
191187

192188
private void processProduces() {
193189

194-
if (produces == null) {
195-
produces = parentContext.getProduces();
196-
}
197-
198-
if (produces == null) {
190+
if (getProduces() == null) {
199191
return;
200192
}
201193

@@ -360,18 +352,23 @@ public Map<String, Object> getExtensions() {
360352
}
361353

362354
public String[] getConsumers() {
363-
return consumers;
355+
return consumes;
364356
}
365357

366-
public void setConsumers(String[] consumers) {
367-
this.consumers = consumers;
358+
public void setConsumers(String[] consumes) {
359+
this.consumes = consumes;
368360
}
369361

370362
public void addParamCtx(ParameterContext ctx) {
371363
this.parameterContexts.add(ctx);
372364
}
373365

374366
public String[] getProduces() {
367+
368+
if (produces == null) {
369+
produces = parentContext.getProduces();
370+
}
371+
375372
return produces;
376373
}
377374

@@ -380,6 +377,11 @@ public void setProduces(String[] produces) {
380377
}
381378

382379
public String[] getHeaders() {
380+
381+
if (headers == null) {
382+
headers = parentContext.getHeaders();
383+
}
384+
383385
return headers;
384386
}
385387

oas-generator/oas-generator-core/src/main/java/org/apache/servicecomb/toolkit/generator/context/ParameterContext.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class ParameterContext implements ISchemaContext, IExtensionsContext {
8282

8383
private RequestBody requestBody;
8484

85-
private List<String> consumers;
85+
private List<String> consumes;
8686

8787
public ParameterContext(OperationContext parentContext, Parameter parameter) {
8888
this.parentContext = parentContext;
@@ -208,14 +208,14 @@ public Type getType() {
208208
}
209209

210210
public void addConsume(String consume) {
211-
if (consumers == null) {
212-
consumers = new ArrayList<>();
211+
if (consumes == null) {
212+
consumes = new ArrayList<>();
213213
}
214-
consumers.add(consume);
214+
consumes.add(consume);
215215
}
216216

217217
public List<String> getConsumers() {
218-
return consumers;
218+
return consumes;
219219
}
220220

221221
public Type getRealType() {

oas-generator/oas-generator-spring/src/test/java/org/apache/servicecomb/toolkit/generator/SpringAnnotationProcessorTest.java

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,90 @@
5656
public class SpringAnnotationProcessorTest {
5757

5858
@Test
59-
public void getHttpMethod() throws NoSuchMethodException {
60-
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
59+
public void methodOfRequestMapping() throws NoSuchMethodException {
6160

61+
// class level
62+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
6263
OasContext oasContext = new OasContext(parser);
6364
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
6465
RequestMapping requestMappingClassAnnotation = httpMethodResourceClass.getAnnotation(RequestMapping.class);
6566
RequestMappingClassAnnotationProcessor requestMappingClassAnnotationProcessor = new RequestMappingClassAnnotationProcessor();
6667
requestMappingClassAnnotationProcessor.process(requestMappingClassAnnotation, oasContext);
67-
Assert.assertEquals(requestMappingClassAnnotation.value()[0], oasContext.getBasePath());
6868
Assert.assertEquals(RequestMethod.GET.name(), oasContext.getHttpMethod());
6969

70+
// method level
7071
RequestMappingMethodAnnotationProcessor requestMappingMethodAnnotationProcessor = new RequestMappingMethodAnnotationProcessor();
7172
Method requestMethod = httpMethodResourceClass.getMethod("request");
7273
RequestMapping requestMappingMethodAnnotation = requestMethod.getAnnotation(RequestMapping.class);
7374
OperationContext requestOperationContext = new OperationContext(requestMethod, oasContext);
7475
requestMappingMethodAnnotationProcessor.process(requestMappingMethodAnnotation, requestOperationContext);
75-
Assert
76-
.assertEquals(requestMappingMethodAnnotation.value()[0],
77-
requestOperationContext.getPath());
7876
Assert.assertEquals(RequestMethod.POST.name(), requestOperationContext.getHttpMethod());
7977

78+
// default
8079
Method getRequestMethod = httpMethodResourceClass.getMethod("getRequest");
8180
RequestMapping getRequestMappingMethodAnnotation = getRequestMethod.getAnnotation(RequestMapping.class);
8281
OperationContext getRequestOperationContext = new OperationContext(getRequestMethod, oasContext);
8382
requestMappingMethodAnnotationProcessor.process(getRequestMappingMethodAnnotation, getRequestOperationContext);
84-
Assert
85-
.assertEquals(getRequestMappingMethodAnnotation.value()[0],
86-
getRequestOperationContext.getPath());
8783
Assert.assertEquals(RequestMethod.GET.name(), getRequestOperationContext.getHttpMethod());
84+
}
85+
86+
@Test
87+
public void pathOfRequestMapping() {
88+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
89+
OasContext oasContext = new OasContext(parser);
90+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
91+
RequestMapping requestMappingClassAnnotation = httpMethodResourceClass.getAnnotation(RequestMapping.class);
92+
RequestMappingClassAnnotationProcessor requestMappingClassAnnotationProcessor = new RequestMappingClassAnnotationProcessor();
93+
requestMappingClassAnnotationProcessor.process(requestMappingClassAnnotation, oasContext);
94+
Assert.assertEquals(requestMappingClassAnnotation.value()[0], oasContext.getBasePath());
95+
}
96+
97+
@Test
98+
public void headersOfRequestMapping() throws NoSuchMethodException {
99+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
100+
OasContext oasContext = new OasContext(parser);
101+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
102+
RequestMappingMethodAnnotationProcessor requestMappingMethodAnnotationProcessor = new RequestMappingMethodAnnotationProcessor();
103+
Method requestMethod = httpMethodResourceClass.getMethod("request");
104+
RequestMapping requestMappingMethodAnnotation = requestMethod.getAnnotation(RequestMapping.class);
105+
OperationContext requestOperationContext = new OperationContext(requestMethod, oasContext);
106+
requestMappingMethodAnnotationProcessor.process(requestMappingMethodAnnotation, requestOperationContext);
107+
Assert.assertArrayEquals(requestMappingMethodAnnotation.headers(), requestOperationContext.getHeaders());
108+
}
109+
110+
@Test
111+
public void consumesOfRequestMapping() throws NoSuchMethodException {
112+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
113+
OasContext oasContext = new OasContext(parser);
114+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
115+
RequestMappingMethodAnnotationProcessor requestMappingMethodAnnotationProcessor = new RequestMappingMethodAnnotationProcessor();
116+
Method requestMethod = httpMethodResourceClass.getMethod("request");
117+
RequestMapping requestMappingMethodAnnotation = requestMethod.getAnnotation(RequestMapping.class);
118+
OperationContext requestOperationContext = new OperationContext(requestMethod, oasContext);
119+
requestMappingMethodAnnotationProcessor.process(requestMappingMethodAnnotation, requestOperationContext);
120+
Assert.assertArrayEquals(requestMappingMethodAnnotation.consumes(), requestOperationContext.getConsumers());
121+
}
122+
123+
@Test
124+
public void producesOfRequestMapping() throws NoSuchMethodException {
125+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
126+
OasContext oasContext = new OasContext(parser);
127+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
128+
RequestMappingMethodAnnotationProcessor requestMappingMethodAnnotationProcessor = new RequestMappingMethodAnnotationProcessor();
129+
Method requestMethod = httpMethodResourceClass.getMethod("request");
130+
RequestMapping requestMappingMethodAnnotation = requestMethod.getAnnotation(RequestMapping.class);
131+
OperationContext requestOperationContext = new OperationContext(requestMethod, oasContext);
132+
requestMappingMethodAnnotationProcessor.process(requestMappingMethodAnnotation, requestOperationContext);
133+
Assert.assertArrayEquals(requestMappingMethodAnnotation.produces(), requestOperationContext.getProduces());
134+
}
135+
136+
137+
@Test
138+
public void methodOfGetMapping() throws NoSuchMethodException {
139+
140+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
141+
OasContext oasContext = new OasContext(parser);
142+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
88143

89144
GetMappingMethodAnnotationProcessor getMappingMethodAnnotationProcessor = new GetMappingMethodAnnotationProcessor();
90145
Method getMethod = httpMethodResourceClass.getMethod("get");
@@ -93,6 +148,14 @@ public void getHttpMethod() throws NoSuchMethodException {
93148
getMappingMethodAnnotationProcessor.process(getMappingAnnotation, getOperationContext);
94149
Assert
95150
.assertEquals(getMappingAnnotation.value()[0], getOperationContext.getPath());
151+
}
152+
153+
@Test
154+
public void methodOfPostMapping() throws NoSuchMethodException {
155+
156+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
157+
OasContext oasContext = new OasContext(parser);
158+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
96159

97160
PostMappingMethodAnnotationProcessor postMappingMethodAnnotationProcessor = new PostMappingMethodAnnotationProcessor();
98161
Method postMethod = httpMethodResourceClass.getMethod("post");
@@ -101,6 +164,14 @@ public void getHttpMethod() throws NoSuchMethodException {
101164
postMappingMethodAnnotationProcessor.process(postMappingAnnotation, postOperationContext);
102165
Assert
103166
.assertEquals(postMappingAnnotation.value()[0], postOperationContext.getPath());
167+
}
168+
169+
@Test
170+
public void methodOfPutMapping() throws NoSuchMethodException {
171+
172+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
173+
OasContext oasContext = new OasContext(parser);
174+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
104175

105176
PutMappingMethodAnnotationProcessor putMappingMethodAnnotationProcessor = new PutMappingMethodAnnotationProcessor();
106177
Method putMethod = httpMethodResourceClass.getMethod("put");
@@ -109,6 +180,13 @@ public void getHttpMethod() throws NoSuchMethodException {
109180
putMappingMethodAnnotationProcessor.process(putMappingAnnotation, putOperationContext);
110181
Assert
111182
.assertEquals(putMappingAnnotation.value()[0], putOperationContext.getPath());
183+
}
184+
185+
@Test
186+
public void methodOfDeleteMapping() throws NoSuchMethodException {
187+
SpringmvcAnnotationParser parser = new SpringmvcAnnotationParser();
188+
OasContext oasContext = new OasContext(parser);
189+
Class<HttpMethodResource> httpMethodResourceClass = HttpMethodResource.class;
112190

113191
DeleteMappingMethodAnnotationProcessor deleteMappingMethodAnnotationProcessor = new DeleteMappingMethodAnnotationProcessor();
114192
Method deleteMethod = httpMethodResourceClass.getMethod("delete");
@@ -195,7 +273,7 @@ public void interceptorModel() {
195273
@RequestMapping(value = "/path", method = RequestMethod.GET)
196274
class HttpMethodResource {
197275

198-
@RequestMapping(value = "/request", method = RequestMethod.POST, headers = "cookie=1")
276+
@RequestMapping(value = "/request", method = RequestMethod.POST, headers = "cookie=1", consumes = "application/json", produces = "application/json")
199277
public String request() {
200278
return "request";
201279
}

0 commit comments

Comments
 (0)