Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f54cfa9

Browse files
committed
Demonstrated intricate generics support
resolves springfox#1631
1 parent 4a0a863 commit f54cfa9

9 files changed

Lines changed: 258 additions & 31 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
*
3+
* Copyright 2017-2018 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.spring.web.dummy.controllers;
20+
21+
public class CommonRestController {
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* Copyright 2017-2018 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.spring.web.dummy.controllers;
20+
21+
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.ResponseEntity;
23+
import org.springframework.stereotype.Controller;
24+
import org.springframework.web.bind.annotation.PostMapping;
25+
import org.springframework.web.bind.annotation.RequestMapping;
26+
import springfox.documentation.spring.web.dummy.models.Pet;
27+
28+
@Controller
29+
@RequestMapping("/generic/pets")
30+
public class GenericPetController extends GenericRestController<Pet, PetRepository> {
31+
@PostMapping("test")
32+
public ResponseEntity<Pet> test() {
33+
return new ResponseEntity(HttpStatus.OK);
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
*
3+
* Copyright 2017-2018 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.spring.web.dummy.controllers;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.http.ResponseEntity;
23+
import springfox.documentation.spring.web.dummy.models.Pet;
24+
25+
public abstract class GenericRestController<T extends Pet, S extends IRepository<T>>
26+
extends CommonRestController {
27+
28+
@Autowired
29+
protected S genericService;
30+
31+
public abstract ResponseEntity<T> test();
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
*
3+
* Copyright 2017-2018 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.spring.web.dummy.controllers;
20+
21+
public interface IRepository<T> {
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
*
3+
* Copyright 2017-2018 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.spring.web.dummy.controllers;
20+
21+
import org.springframework.stereotype.Component;
22+
import springfox.documentation.spring.web.dummy.models.Pet;
23+
24+
@Component
25+
class PetRepository implements IRepository<Pet> {
26+
}

springfox-swagger-common/src/test/groovy/springfox/documentation/swagger/web/ApiResourceControllerSpec.groovy

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1+
/*
2+
*
3+
* Copyright 2017-2018 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
119
package springfox.documentation.swagger.web
220

321
import com.fasterxml.jackson.databind.ObjectMapper
422
import com.fasterxml.jackson.databind.PropertyNamingStrategy
23+
import org.skyscreamer.jsonassert.JSONAssert
24+
import org.skyscreamer.jsonassert.JSONCompareMode
525
import org.springframework.http.MediaType
626
import org.springframework.test.web.servlet.setup.MockMvcBuilders
7-
import spock.lang.Ignore
827
import spock.lang.Specification
928
import springfox.documentation.builders.DocumentationBuilder
1029
import springfox.documentation.service.ApiInfo
@@ -16,6 +35,44 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
1635

1736
class ApiResourceControllerSpec extends Specification {
1837
def mockMvc
38+
def security = "{\n" +
39+
" \"apiKey\": \"key\",\n" +
40+
" \"apiKeyName\": \"api_key\",\n" +
41+
" \"apiKeyVehicle\": \"header\",\n" +
42+
" \"appName\": \"test\",\n" +
43+
" \"clientId\": \"client\",\n" +
44+
" \"clientSecret\": \"client-secret\",\n" +
45+
" \"realm\": \"real\",\n" +
46+
" \"scopeSeparator\": \",\"\n" +
47+
"}"
48+
def ui = "{\n" +
49+
" \"apisSorter\": \"alpha\",\n" +
50+
" \"defaultModelRendering\": \"schema\",\n" +
51+
" \"docExpansion\": \"none\",\n" +
52+
" \"jsonEditor\": false,\n" +
53+
" \"showRequestHeaders\": true,\n" +
54+
" \"supportedSubmitMethods\": [\n" +
55+
" \"get\",\n" +
56+
" \"post\",\n" +
57+
" \"put\",\n" +
58+
" \"delete\",\n" +
59+
" \"patch\"\n" +
60+
" ],\n" +
61+
" \"validatorUrl\": \"/validate\"\n" +
62+
"}"
63+
def resources = "[\n" +
64+
" {\n" +
65+
" \"name\": \"test\",\n" +
66+
" \"location\": \"/v1?group=test\",\n" +
67+
" \"swaggerVersion\": \"1.2\"\n" +
68+
" },\n" +
69+
" {\n" +
70+
" \"name\": \"test\",\n" +
71+
" \"location\": \"/v2?group=test\",\n" +
72+
" \"swaggerVersion\": \"2.0\"\n" +
73+
" }\n" +
74+
" ]"
75+
1976
def sut
2077

2178
def setup() {
@@ -58,49 +115,45 @@ class ApiResourceControllerSpec extends Specification {
58115
cache
59116
}
60117

61-
def "security Configuration is available" (){
118+
def "security Configuration is available"() {
62119
expect:
63-
mockMvc.perform(get("/swagger-resources/configuration/security")
120+
mockMvc.perform(get("/swagger-resources/configuration/security")
64121
.accept(MediaType.APPLICATION_JSON))
65-
.andExpect(content().string("{\"clientId\":\"client\",\"clientSecret\":\"client-secret\",\"realm\":\"real\"," +
66-
"\"appName\":\"test\",\"apiKey\":\"key\",\"apiKeyVehicle\":\"header\",\"scopeSeparator\":\",\",\"apiKeyName\":\"api_key\"}"))
122+
.andExpect(content().json(security))
67123
}
68124

69-
def "UI Configuration is available" (){
125+
def "UI Configuration is available"() {
70126
expect:
71127
mockMvc.perform(get("/swagger-resources/configuration/ui")
72128
.accept(MediaType.APPLICATION_JSON))
73-
.andExpect(content().string("{\"validatorUrl\":\"/validate\",\"docExpansion\":\"none\",\"apisSorter\":\"alpha\",\"defaultModelRendering\":\"schema\",\"requestTimeout\":null,\"supportedSubmitMethods\":[\"get\",\"post\",\"put\",\"delete\",\"patch\"],\"jsonEditor\":false,\"showRequestHeaders\":true}"))
129+
.andExpect(content().json(ui))
74130
}
75131

76-
def "Cache is available" (){
132+
def "Cache is available"() {
77133
expect:
78-
mockMvc.perform(get("/swagger-resources")
134+
mockMvc.perform(get("/swagger-resources")
79135
.accept(MediaType.APPLICATION_JSON))
80-
.andExpect(content().string("[{\"name\":\"test\",\"location\":\"/v1?group=test\",\"swaggerVersion\":\"1.2\"},{\"name\":\"test\",\"location\":\"/v2?group=test\",\"swaggerVersion\":\"2.0\"}]"))
136+
.andExpect(content().json(resources))
81137
}
82138

83-
@Ignore
84-
def "Cache is available when swagger controllers are not available" (){
139+
def "Verify that the property naming strategy does not affect output"() {
85140
given:
86-
sut.swagger1Available = false
87-
sut.swagger2Available = false
88-
expect:
89-
mockMvc.perform(get("/swagger-resources")
90-
.accept(MediaType.APPLICATION_JSON))
91-
.andExpect(content().string("[]"))
92-
}
93-
94-
def "Verify that the property naming strategy does not affect output" () {
95-
given:
96-
ObjectMapper mapper = new ObjectMapper()
141+
ObjectMapper mapper = new ObjectMapper()
97142
when:
98-
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
143+
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
144+
99145
then:
100-
mapper.writer().writeValueAsString(sut.securityConfiguration) == "{\"clientId\":\"client\"," +
101-
"\"clientSecret\":\"client-secret\",\"realm\":\"real\",\"appName\":\"test\",\"apiKey\":\"key\",\"apiKeyVehicle\":\"header\",\"scopeSeparator\":\",\",\"apiKeyName\":\"api_key\"}"
102-
mapper.writer().writeValueAsString(sut.uiConfiguration) == "{\"validatorUrl\":\"/validate\",\"docExpansion\":\"none\",\"apisSorter\":\"alpha\",\"defaultModelRendering\":\"schema\",\"requestTimeout\":null,\"supportedSubmitMethods\":[\"get\",\"post\",\"put\",\"delete\",\"patch\"],\"jsonEditor\":false,\"showRequestHeaders\":true}"
103-
mapper.writer().writeValueAsString(sut.swaggerResources().body) == "[{\"name\":\"test\"," +
104-
"\"location\":\"/v1?group=test\",\"swaggerVersion\":\"1.2\"},{\"name\":\"test\",\"location\":\"/v2?group=test\",\"swaggerVersion\":\"2.0\"}]"
146+
JSONAssert.assertEquals(
147+
mapper.writer().writeValueAsString(sut.securityConfiguration),
148+
security,
149+
JSONCompareMode.NON_EXTENSIBLE)
150+
JSONAssert.assertEquals(
151+
mapper.writer().writeValueAsString(sut.uiConfiguration),
152+
ui,
153+
JSONCompareMode.NON_EXTENSIBLE)
154+
JSONAssert.assertEquals(
155+
mapper.writer().writeValueAsString(sut.swaggerResources().body),
156+
resources,
157+
JSONCompareMode.NON_EXTENSIBLE)
105158
}
106159
}

swagger-contract-tests/src/test/groovy/springfox/test/contract/swaggertests/Swagger2TestConfig.groovy

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import static springfox.documentation.schema.AlternateTypeRules.*
2525
public class Swagger2TestConfig {
2626

2727
@Autowired
28-
private TypeResolver resolver;
28+
private TypeResolver resolver
2929

3030
@Bean
3131
public Docket petstore(List<SecurityScheme> authorizationTypes) {
@@ -35,7 +35,11 @@ public class Swagger2TestConfig {
3535
.securitySchemes(authorizationTypes)
3636
.produces(['application/xml', 'application/json'] as Set)
3737
.select()
38-
.paths(and(regex("/api/.*"), not(regex("/api/store/search.*"))))
38+
.paths(or(
39+
and(
40+
regex("/api/.*"),
41+
not(regex("/api/store/search.*"))),
42+
regex("/generic/.*")))
3943
.build()
4044
.host("petstore.swagger.io")
4145
.protocols(['http', 'https'] as Set)

swagger-contract-tests/src/test/resources/contract/swagger/resource-listing.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
"path": "/default/feature-demonstration-service",
4242
"position": 0
4343
},
44+
{
45+
"description": "Generic Pet Controller",
46+
"path": "/default/generic-pet-controller",
47+
"position": 0
48+
},
4449
{
4550
"description": "Groovy Service",
4651
"path": "/default/groovy-service",

swagger-contract-tests/src/test/resources/contract/swagger2/swagger.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
"name": "pet-store-resource",
2020
"description": "Operations about store"
2121
},
22+
{
23+
"name": "generic-pet-controller",
24+
"description": "Generic Pet Controller"
25+
},
2226
{
2327
"name": "pet-controller",
2428
"description": "Operations about pets"
@@ -733,6 +737,30 @@
733737
}
734738
}
735739
}
740+
},
741+
"/generic/pets/test": {
742+
"post": {
743+
"tags": [
744+
"generic-pet-controller"
745+
],
746+
"summary": "test",
747+
"operationId": "testUsingPOST_1",
748+
"consumes": [
749+
"application/json"
750+
],
751+
"produces": [
752+
"application/xml",
753+
"application/json"
754+
],
755+
"responses": {
756+
"200": {
757+
"description": "OK",
758+
"schema": {
759+
"$ref": "#/definitions/Pet"
760+
}
761+
}
762+
}
763+
}
736764
}
737765
},
738766
"securityDefinitions": {

0 commit comments

Comments
 (0)