Skip to content

Commit

Permalink
Updated sample OAS service
Browse files Browse the repository at this point in the history
This is to develop the against a fixed contract that we're expecting in the end

(2022)
  • Loading branch information
dilipkrish committed Jun 23, 2020
1 parent 9b7937f commit 42236c4
Show file tree
Hide file tree
Showing 30 changed files with 4,721 additions and 1,365 deletions.
1 change: 0 additions & 1 deletion oas-contract-tests/build.gradle
Expand Up @@ -48,7 +48,6 @@ dependencies {

compile project(':springfox-oas')
compile project(':springfox-data-rest')
compile project(':springfox-petstore')
compile project(':springfox-bean-validators')

compile(project(path: ':springfox-swagger-ui')) {
Expand Down
Expand Up @@ -21,23 +21,18 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.oas.annotations.EnableOasWebMvc;
import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration;
import springfox.petstore.PetStoreConfiguration;

@SpringBootApplication
@SuppressWarnings("HideUtilityClassConstructor")
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@ComponentScan(basePackages = {
"springfox.test.contract.oas",
"springfox.petstore.controller"
})
@EnableOasWebMvc
@Import(value = {
SpringDataRestConfiguration.class,
PetStoreConfiguration.class,
SecuritySupport.class,
OpenApiTestConfig.class,
BeanValidatorPluginsConfiguration.class })
Expand Down
@@ -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()));
}
}
@@ -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!");
}
}
}

0 comments on commit 42236c4

Please sign in to comment.