Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is to develop the against a fixed contract that we're expecting in the end (2022)
- Loading branch information
1 parent
9b7937f
commit 42236c4
Showing
30 changed files
with
4,721 additions
and
1,365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
241 changes: 23 additions & 218 deletions
241
oas-contract-tests/src/main/java/springfox/test/contract/oas/OpenApiTestConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,248 +1,53 @@ | ||
package springfox.test.contract.oas; | ||
|
||
import groovy.lang.MetaClass; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import springfox.documentation.builders.PathSelectors; | ||
import springfox.documentation.oas.annotations.EnableOasWebMvc; | ||
import springfox.documentation.service.ApiKey; | ||
import springfox.documentation.service.AuthorizationScope; | ||
import springfox.documentation.service.HttpAuthenticationScheme; | ||
import springfox.documentation.service.OAuth2Scheme; | ||
import springfox.documentation.service.SecurityScheme; | ||
import springfox.documentation.spi.DocumentationType; | ||
import springfox.documentation.spring.web.plugins.Docket; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
@Configuration | ||
@EnableOasWebMvc | ||
public class OpenApiTestConfig { | ||
@Bean | ||
public Docket petstoreWithUriTemplating(List<SecurityScheme> authorizationTypes) { | ||
public Docket petStore(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("petstoreTemplated") | ||
.groupName("petstore") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<String>() {{ | ||
add("application/xml"); | ||
add("application/json"); | ||
}}) | ||
.select() | ||
.paths(PathSelectors.regex("/api/store/search.*")) | ||
.paths(PathSelectors.regex("/.*") | ||
.and(PathSelectors.regex("/error").negate()) | ||
.and(PathSelectors.regex("/profile").negate())) | ||
.build() | ||
.enableUrlTemplating(true) | ||
.enableUrlTemplating(false) | ||
.host("petstore.swagger.io") | ||
.protocols(new HashSet<>(Arrays.asList( | ||
"http", | ||
"https"))); | ||
} | ||
|
||
@Bean | ||
public Docket business(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("businessService") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>(Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/business.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket concrete(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("concrete") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>(Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/foo/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket noRequestMapping(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("noRequestMapping") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>(Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/no-request-mapping/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket fancyPetstore(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("fancyPetstore") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>(Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/fancypets/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket inheritedService(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("inheritedService") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>(Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/child/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket pet(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("petService") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>(Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.enableUrlTemplating(true) | ||
.select() | ||
.paths(PathSelectors.regex("/pets/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket petGrooming(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("petGroomingService") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>( | ||
Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/petgrooming/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket root(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("root") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>( | ||
Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.ignoredParameterTypes(MetaClass.class) | ||
.select() | ||
.paths(PathSelectors.regex("/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket groovyServiceBean(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("groovyService") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.forCodeGeneration(true) | ||
.produces(new HashSet<>( | ||
Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/groovy/.*")) | ||
.build() | ||
.ignoredParameterTypes(MetaClass.class); | ||
} | ||
|
||
@Bean | ||
public Docket enumServiceBean(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("enumService") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>( | ||
Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/enums/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket consumesProducesNotOnDocumentContext(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("consumesProducesNotOnDocumentContext") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.select() | ||
.paths(PathSelectors.regex("/consumes-produces/.*")) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket consumesProducesOnDocumentContext(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("consumesProducesOnDocumentContext") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.consumes(new HashSet<String>() {{ | ||
add("text/plain"); | ||
}}) | ||
.produces(new HashSet<String>() {{ | ||
add("application/json"); | ||
}}) | ||
.select().paths(PathSelectors.regex("/consumes-produces/.*")).build(); | ||
} | ||
|
||
@Bean | ||
public Docket springDataRest() { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("spring-data-rest") | ||
.useDefaultResponseMessages(false) | ||
.enableUrlTemplating(true) | ||
.securitySchemes(new ArrayList<>()) | ||
.forCodeGeneration(true) | ||
.produces(new HashSet<>( | ||
Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/rest/people.*") | ||
.or(PathSelectors.regex("/rest/tags.*")) | ||
.or(PathSelectors.regex("/rest/categories.*")) | ||
.or(PathSelectors.regex("/rest/addresses.*"))) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Docket same(List<SecurityScheme> authorizationTypes) { | ||
return new Docket(DocumentationType.OAS_30) | ||
.groupName("same") | ||
.useDefaultResponseMessages(false) | ||
.securitySchemes(authorizationTypes) | ||
.produces(new HashSet<>( | ||
Arrays.asList( | ||
"application/xml", | ||
"application/json"))) | ||
.select() | ||
.paths(PathSelectors.regex("/same/.*")) | ||
.build(); | ||
"https"))) | ||
.securitySchemes(Arrays.asList( | ||
new ApiKey("api_key", "api_key", "header"), | ||
HttpAuthenticationScheme.BASIC_AUTH_BUILDER | ||
.name("basicScheme") | ||
.build(), | ||
OAuth2Scheme.OAUTH2_IMPLICIT_FLOW_BUILDER | ||
.name("petstore_auth") | ||
.authorizationUrl("https://petstore3.swagger.io/oauth/authorize") | ||
.scopes(Arrays.asList( | ||
new AuthorizationScope("write:pets", "Write scope"), | ||
new AuthorizationScope("read:pets", "Read scope"))) | ||
.build())); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
oas-contract-tests/src/main/java/springfox/test/contract/oas/api/ApiUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* | ||
* * Copyright 2019-2020 the original author or authors. | ||
* * | ||
* * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* * you may not use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * https://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
*/ | ||
|
||
package springfox.test.contract.oas.api; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class ApiUtil { | ||
private ApiUtil() { | ||
} | ||
|
||
public static void setExampleResponse( | ||
NativeWebRequest req, | ||
String contentType, | ||
String example) { | ||
try { | ||
req.getNativeResponse(HttpServletResponse.class).addHeader("Content-Type", contentType); | ||
req.getNativeResponse(HttpServletResponse.class).getOutputStream().print(example); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static void checkApiKey(NativeWebRequest req) { | ||
if (!"1".equals(System.getenv("DISABLE_API_KEY")) | ||
&& !"special-key".equals(req.getHeader("api_key"))) { | ||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Missing API key!"); | ||
} | ||
} | ||
} |
Oops, something went wrong.