Skip to content

Commit

Permalink
refs swagger-api#2312 - reorganize modules
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Aug 2, 2017
1 parent b979a56 commit 1a095a6
Show file tree
Hide file tree
Showing 39 changed files with 183 additions and 261 deletions.
2 changes: 1 addition & 1 deletion modules/swagger-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-web</artifactId>
<artifactId>swagger-models</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.swagger.oas.integration;

import io.swagger.oas.integration.api.OpenAPIConfiguration;

import java.io.IOException;

public class ClasspathOpenApiConfigurationLoader implements StringOpenApiConfigurationLoader {

@Override
public OpenApiConfiguration load(String path) throws IOException {
public OpenAPIConfiguration load(String path) throws IOException {
String sanitized = (path.startsWith("/") ? path : "/" + path);
String configString = readInputStreamToString(this.getClass().getResource(sanitized).openStream());
return deserializeConfig(path, configString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.swagger.oas.integration;

import io.swagger.oas.web.OpenAPIConfig;
import io.swagger.oas.integration.api.OpenAPIConfiguration;
import io.swagger.util.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -9,30 +9,16 @@ public class ContextUtils {

private static Logger LOGGER = LoggerFactory.getLogger(ContextUtils.class);

public static OpenApiConfiguration deepCopy (OpenApiConfiguration config) {
public static OpenAPIConfiguration deepCopy (OpenAPIConfiguration config) {
if (config == null) {
return null;
}
try {
return Json.mapper().readValue(Json.pretty(config), OpenApiConfiguration.class);
return Json.mapper().readValue(Json.pretty(config), OpenApiConfigurationImpl.class);
} catch (Exception e) {
LOGGER.error("Exception cloning config: " + e.getMessage(), e);
return config;
}
}

public static OpenApiConfiguration cloneConfigFromInterface(OpenAPIConfig configInterface) {

return new OpenApiConfiguration()
.openApi(configInterface.getOpenAPI())
.userDefinedOptions(configInterface.getUserDefinedOptions())
.filterClass(configInterface.getFilterClass())
.prettyPrint(configInterface.isPrettyPrint())
.readerClass(configInterface.getReaderClass())
.resourcePackages(configInterface.getResourcePackages())
.resourceClasses(configInterface.getResourceClasses())
.scanAllResources(configInterface.isScanAllResources())
.scannerClass(configInterface.getScannerClass());
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.swagger.oas.integration;

import io.swagger.oas.integration.api.OpenAPIConfiguration;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileOpenApiConfigurationLoader implements StringOpenApiConfigurationLoader {

@Override
public OpenApiConfiguration load(String path) throws IOException {
public OpenAPIConfiguration load(String path) throws IOException {
File file = new File(path);
return deserializeConfig(path, readInputStreamToString(new FileInputStream(file)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.swagger.oas.integration;

import io.swagger.oas.integration.api.OpenApiConfigurationLoader;
import io.swagger.oas.integration.api.OpenApiContext;
import io.swagger.oas.models.OpenAPI;
import io.swagger.oas.web.OpenAPIConfig;
import io.swagger.oas.web.OpenApiReader;
import io.swagger.oas.web.OpenApiScanner;
import io.swagger.oas.integration.api.OpenAPIConfiguration;
import io.swagger.oas.integration.api.OpenApiReader;
import io.swagger.oas.integration.api.OpenApiScanner;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.slf4j.Logger;
Expand All @@ -26,7 +28,7 @@ public class GenericOpenApiContext<T extends GenericOpenApiContext> implements O
protected String id = OPENAPI_CONTEXT_ID_DEFAULT;
protected OpenApiContext parent;
protected String configLocation;
private OpenApiConfiguration openApiConfiguration;
private OpenAPIConfiguration openApiConfiguration;

private OpenApiReader openApiReader;
private OpenApiScanner openApiScanner;
Expand Down Expand Up @@ -104,7 +106,7 @@ public T resourceClasses(Set<String> resourceClasses) {
return (T) this;
}

public T openApiConfiguration(OpenApiConfiguration openApiConfiguration) {
public T openApiConfiguration(OpenAPIConfiguration openApiConfiguration) {
this.openApiConfiguration = openApiConfiguration;
return (T) this;
}
Expand Down Expand Up @@ -155,25 +157,25 @@ protected void register() {
}

@Override
public OpenApiConfiguration getOpenApiConfiguration() {
public OpenAPIConfiguration getOpenApiConfiguration() {
return openApiConfiguration;
}

public void setOpenApiConfiguration(OpenApiConfiguration openApiConfiguration) {
public void setOpenApiConfiguration(OpenAPIConfiguration openApiConfiguration) {
this.openApiConfiguration = openApiConfiguration;
}

protected OpenApiReader buildReader(final OpenApiConfiguration openApiConfiguration) throws Exception {
protected OpenApiReader buildReader(final OpenAPIConfiguration openApiConfiguration) throws Exception {
OpenApiReader reader;
if (StringUtils.isNotBlank(openApiConfiguration.getReaderClass())) {
Class cls = getClass().getClassLoader().loadClass(openApiConfiguration.getReaderClass());
reader = (OpenApiReader) cls.newInstance();
} else {
reader = new OpenApiReader() {

OpenAPIConfig openApiConfiguration;
OpenAPIConfiguration openApiConfiguration;
@Override
public void setConfiguration(OpenAPIConfig openApiConfiguration) {
public void setConfiguration(OpenAPIConfiguration openApiConfiguration) {
this.openApiConfiguration = openApiConfiguration;
}

Expand All @@ -188,7 +190,7 @@ public OpenAPI read(Set<Class<?>> classes, Map<String, Object> resources) {
return reader;
}

protected OpenApiScanner buildScanner(final OpenApiConfiguration openApiConfiguration) throws Exception {
protected OpenApiScanner buildScanner(final OpenAPIConfiguration openApiConfiguration) throws Exception {
OpenApiScanner scanner;
if (StringUtils.isNotBlank(openApiConfiguration.getScannerClass())) {
Class cls = getClass().getClassLoader().loadClass(openApiConfiguration.getScannerClass());
Expand Down Expand Up @@ -219,7 +221,7 @@ protected Map<String, OpenApiConfigurationLoader> getLocationLoaders() {
return map;
}

protected OpenApiConfiguration loadConfiguration() throws OpenApiConfigurationException{
protected OpenAPIConfiguration loadConfiguration() throws OpenApiConfigurationException{

Map<String, OpenApiConfigurationLoader> loaders = getLocationLoaders();
try {
Expand Down Expand Up @@ -257,11 +259,11 @@ public T init() throws OpenApiConfigurationException{
}

if (openApiConfiguration == null) {
openApiConfiguration = new OpenApiConfiguration().resourcePackages(resourcePackages);
openApiConfiguration.setId(id);
openApiConfiguration = new OpenApiConfigurationImpl().resourcePackages(resourcePackages);
((OpenApiConfigurationImpl)openApiConfiguration).setId(id);
}

mergeParentConfiguration(openApiConfiguration, parent);
openApiConfiguration = mergeParentConfiguration(openApiConfiguration, parent);

try {
if (openApiReader == null) {
Expand All @@ -283,42 +285,54 @@ public T init() throws OpenApiConfigurationException{
return (T) this;
}

private void mergeParentConfiguration (OpenApiConfiguration config, OpenApiContext parent) {
private OpenAPIConfiguration mergeParentConfiguration (OpenAPIConfiguration config, OpenApiContext parent) {
if (parent == null || parent.getOpenApiConfiguration() == null) {
return;
return config;
}
OpenApiConfiguration parentConfig = parent.getOpenApiConfiguration();
if (config.getResourceClasses() == null) {
config.setResourceClasses(parentConfig.getResourceClasses());
OpenAPIConfiguration parentConfig = parent.getOpenApiConfiguration();

OpenApiConfigurationImpl merged = null;

if (config instanceof OpenApiConfigurationImpl) {
merged = (OpenApiConfigurationImpl)config;
} else {
merged = (OpenApiConfigurationImpl)ContextUtils.deepCopy(config);
}
if (config.getFilterClass() == null) {
config.setFilterClass(parentConfig.getFilterClass());

if (merged.getResourceClasses() == null) {
merged.setResourceClasses(parentConfig.getResourceClasses());
}
if (config.getIgnoredRoutes() == null) {
config.setIgnoredRoutes(parentConfig.getIgnoredRoutes());
if (merged.getFilterClass() == null) {
merged.setFilterClass(parentConfig.getFilterClass());
}
if (config.getOpenAPI() == null) {
config.setOpenApi(parentConfig.getOpenAPI());
if (merged.getIgnoredRoutes() == null) {
merged.setIgnoredRoutes(parentConfig.getIgnoredRoutes());
}
if (config.getReaderClass() == null) {
config.setReaderClass(parentConfig.getReaderClass());
if (merged.getOpenAPI() == null) {
merged.setOpenApi(parentConfig.getOpenAPI());
}
if (config.getResourcePackages() == null) {
config.setResourcePackages(parentConfig.getResourcePackages());
if (merged.getReaderClass() == null) {
merged.setReaderClass(parentConfig.getReaderClass());
}
if (config.getScannerClass() == null) {
config.setScannerClass(parentConfig.getScannerClass());
if (merged.getResourcePackages() == null) {
merged.setResourcePackages(parentConfig.getResourcePackages());
}
if (config.getUserDefinedOptions() == null) {
config.setUserDefinedOptions(parentConfig.getUserDefinedOptions());
if (merged.getScannerClass() == null) {
merged.setScannerClass(parentConfig.getScannerClass());
}
if (config.isPrettyPrint() == null) {
config.setPrettyPrint(parentConfig.isPrettyPrint());
if (merged.getCacheTTL() == null) {
merged.setCacheTTL(parentConfig.getCacheTTL());
}
if (config.isScanAllResources() == null) {
config.setScanAllResources(parentConfig.isScanAllResources());
if (merged.getUserDefinedOptions() == null) {
merged.setUserDefinedOptions(parentConfig.getUserDefinedOptions());
}

if (merged.isPrettyPrint() == null) {
merged.setPrettyPrint(parentConfig.isPrettyPrint());
}
if (merged.isScanAllResources() == null) {
merged.setScanAllResources(parentConfig.isScanAllResources());
}
return merged;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.swagger.oas.integration;

import io.swagger.oas.integration.api.OpenAPIConfiguration;
import io.swagger.oas.integration.api.OpenApiContext;
import io.swagger.oas.integration.api.OpenApiContextBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;
Expand All @@ -10,7 +13,7 @@ public class GenericOpenApiContextBuilder<T extends GenericOpenApiContextBuilder

protected String configLocation;
protected Set<String> resourcePackages;
protected OpenApiConfiguration openApiConfiguration;
protected OpenAPIConfiguration openApiConfiguration;


@Override
Expand Down Expand Up @@ -64,11 +67,11 @@ public void setResourcePackages(Set<String> resourcePackages) {
this.resourcePackages = resourcePackages;
}

public OpenApiConfiguration getOpenApiConfiguration() {
public OpenAPIConfiguration getOpenApiConfiguration() {
return openApiConfiguration;
}

public void setOpenApiConfiguration(OpenApiConfiguration openApiConfiguration) {
public void setOpenApiConfiguration(OpenAPIConfiguration openApiConfiguration) {
this.openApiConfiguration = openApiConfiguration;
}

Expand All @@ -88,7 +91,7 @@ public T resourcePackages(Set<String> resourcePackages) {
return (T) this;
}

public T openApiConfiguration(OpenApiConfiguration openApiConfiguration) {
public T openApiConfiguration(OpenAPIConfiguration openApiConfiguration) {
this.openApiConfiguration = openApiConfiguration;
return (T) this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.swagger.oas.integration;

import io.swagger.oas.web.OpenAPIConfig;
import io.swagger.oas.web.OpenApiScanner;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import io.swagger.oas.integration.api.OpenAPIConfiguration;
import io.swagger.oas.integration.api.OpenApiScanner;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
Expand All @@ -21,11 +19,11 @@ public class GenericOpenApiScanner implements OpenApiScanner {

private static Logger LOGGER = LoggerFactory.getLogger(GenericOpenApiScanner.class);

OpenApiConfiguration openApiConfiguration;
OpenAPIConfiguration openApiConfiguration;

@Override
public void setConfiguration(OpenAPIConfig openApiConfiguration) {
this.openApiConfiguration = ContextUtils.cloneConfigFromInterface(openApiConfiguration);
public void setConfiguration(OpenAPIConfiguration openApiConfiguration) {
this.openApiConfiguration = openApiConfiguration;
}

@Override
Expand Down
Loading

0 comments on commit 1a095a6

Please sign in to comment.