Skip to content

Commit 8935bed

Browse files
committed
Merge pull request #15 from swagger-api/develop
merged from develop
2 parents 4b8d34b + 278e8a2 commit 8935bed

File tree

8 files changed

+383
-12
lines changed

8 files changed

+383
-12
lines changed

modules/swagger-compat-spec-parser/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<parent>
44
<groupId>io.swagger</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>1.0.0</version>
6+
<version>1.0.1</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<groupId>io.swagger</groupId>
1111
<artifactId>swagger-compat-spec-parser</artifactId>
12-
<version>1.0.0</version>
12+
<version>1.0.1</version>
1313
<packaging>jar</packaging>
1414
<name>swagger-compat-spec-parser</name>
1515
<dependencies>

modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public Swagger read(String input) throws IOException {
5252

5353
public Swagger read(String input, List<AuthorizationValue> auths) throws IOException {
5454
Swagger output = null;
55-
5655
MessageBuilder migrationMessages = new MessageBuilder();
5756
SwaggerLegacyParser swaggerParser = new SwaggerLegacyParser();
5857
ResourceListing resourceListing = null;
@@ -422,7 +421,13 @@ else if(resourceListing.getApiVersion() != null) {
422421
String basePath = null;
423422

424423
for(ApiDeclaration apiDeclaration : apiDeclarations) {
425-
String tag = apiDeclaration.getResourcePath();
424+
String tag;
425+
if (apiDeclaration.getApiListingRef() != null) {
426+
String refPath = apiDeclaration.getApiListingRef().getPath();
427+
tag = refPath.substring(refPath.lastIndexOf("/") + 1);
428+
} else {
429+
tag = apiDeclaration.getResourcePath();
430+
}
426431
if(tag != null) {
427432
tag = tag.replaceAll("/", "");
428433
}

modules/swagger-parser/pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<parent>
44
<groupId>io.swagger</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>1.0.0</version>
6+
<version>1.0.1</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<groupId>io.swagger</groupId>
1111
<artifactId>swagger-parser</artifactId>
12-
<version>1.0.0</version>
12+
<version>1.0.1</version>
1313
<packaging>jar</packaging>
1414
<name>swagger-parser</name>
1515
<dependencies>
@@ -30,5 +30,15 @@
3030
<version>${junit-version}</version>
3131
<scope>test</scope>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-ext</artifactId>
36+
<version>${slf4j-version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-api</artifactId>
41+
<version>${slf4j-version}</version>
42+
</dependency>
3343
</dependencies>
3444
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.swagger.parser;
2+
3+
public class ResolverOptions {
4+
5+
}

modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
public class SwaggerParser {
1313
public Swagger read(String location) {
14-
return read(location, null);
14+
return read(location, null, true);
1515
}
1616

17-
public Swagger read(String location, List<AuthorizationValue> auths) {
17+
public Swagger read(String location, List<AuthorizationValue> auths, boolean resolve) {
1818
if(location == null)
1919
return null;
2020

@@ -24,7 +24,7 @@ public Swagger read(String location, List<AuthorizationValue> auths) {
2424
try{
2525
output = new Swagger20Parser().read(location, auths);
2626
if(output != null)
27-
return output;
27+
return new SwaggerResolver().resolve(output, auths);
2828
}
2929
catch (IOException e) {
3030
// continue;
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
package io.swagger.parser;
2+
3+
import io.swagger.parser.util.RemoteUrl;
4+
5+
import com.wordnik.swagger.util.Json;
6+
import com.wordnik.swagger.models.*;
7+
import com.wordnik.swagger.models.parameters.*;
8+
import com.wordnik.swagger.models.properties.*;
9+
import com.wordnik.swagger.models.auth.AuthorizationValue;
10+
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import java.util.ServiceLoader;
17+
import java.util.*;
18+
import java.io.IOException;
19+
20+
public class SwaggerResolver {
21+
Logger LOGGER = LoggerFactory.getLogger(SwaggerResolver.class);
22+
protected Swagger swagger;
23+
protected Map<String, ResolutionContext> resolutionMap = new HashMap<String, ResolutionContext>();
24+
25+
protected ResolverOptions opts;
26+
public SwaggerResolver(){}
27+
public SwaggerResolver(ResolverOptions opts) {
28+
this.opts = opts;
29+
}
30+
public Swagger resolve(Swagger swagger, List<AuthorizationValue> auths) {
31+
if(swagger == null)
32+
return null;
33+
34+
this.swagger = swagger;
35+
36+
// models
37+
detectModelRefs();
38+
39+
// operations
40+
detectOperationRefs();
41+
42+
applyResolutions(auths);
43+
return this.swagger;
44+
}
45+
46+
public void applyResolutions(List<AuthorizationValue> auths) {
47+
// hosts to call
48+
Map<String, List<Object>> hostToObjectMap = new HashMap<String, List<Object>>();
49+
50+
for(String path : resolutionMap.keySet()) {
51+
String[] parts = path.split("#");
52+
if(parts.length == 2) {
53+
String host = parts[0];
54+
String definitionPath = parts[1];
55+
List<Object> objectList = hostToObjectMap.get(host);
56+
if(objectList == null) {
57+
objectList = new ArrayList<Object>();
58+
hostToObjectMap.put(host, objectList);
59+
}
60+
ResolutionContext ctx = resolutionMap.get(path);
61+
62+
Object mapping = ctx.object;
63+
Object target = ctx.parent;
64+
try {
65+
String contents = null;
66+
if(host.startsWith("http"))
67+
contents = new RemoteUrl().urlToString(host, auths);
68+
else
69+
contents = Json.mapper().writeValueAsString(swagger);
70+
JsonNode location = null;
71+
String locationName = null;
72+
if(contents != null) {
73+
location = Json.mapper().readTree(contents);
74+
String[] objectPath = definitionPath.split("/");
75+
for(String objectPathPart : objectPath) {
76+
LOGGER.debug("getting part " + objectPathPart);
77+
if(objectPathPart.length() > 0 && location != null) {
78+
location = location.get(objectPathPart);
79+
locationName = objectPathPart;
80+
}
81+
}
82+
}
83+
if(location != null) {
84+
// convert the node to the proper type
85+
if(mapping instanceof Property) {
86+
Model model = Json.mapper().convertValue(location, Model.class);
87+
if(mapping instanceof RefProperty) {
88+
RefProperty ref = (RefProperty) mapping;
89+
ref.set$ref(locationName);
90+
swagger.addDefinition(locationName, model);
91+
}
92+
}
93+
else if(target instanceof Parameter) {
94+
if(mapping instanceof RefModel) {
95+
Model model = Json.mapper().convertValue(location, Model.class);
96+
RefModel ref = (RefModel) mapping;
97+
ref.set$ref(locationName);
98+
swagger.addDefinition(locationName, model);
99+
}
100+
}
101+
else if(target instanceof Operation) {
102+
103+
// get the operation position
104+
Operation operation = (Operation) target;
105+
int position = 0;
106+
for(Parameter param : operation.getParameters()) {
107+
108+
if(param instanceof RefParameter) {
109+
RefParameter ref = (RefParameter) param;
110+
if(ref.getSimpleRef().equals(locationName)) {
111+
// found a match!
112+
Parameter remoteParam = Json.mapper().convertValue(location, Parameter.class);
113+
operation.getParameters().set(position, remoteParam);
114+
}
115+
}
116+
position += 1;
117+
}
118+
}
119+
}
120+
}
121+
catch(Exception e) {
122+
// failed to get it
123+
e.printStackTrace();
124+
}
125+
}
126+
}
127+
}
128+
129+
public void detectOperationRefs() {
130+
Map<String, Path> paths = swagger.getPaths();
131+
if(paths == null) return;
132+
133+
for(String pathName : paths.keySet()) {
134+
Path path = paths.get(pathName);
135+
List<Operation> operations = path.getOperations();
136+
for(Operation operation : operations) {
137+
if(operation.getParameters() != null) {
138+
for(Parameter parameter : operation.getParameters()) {
139+
if(parameter instanceof BodyParameter) {
140+
BodyParameter bp = (BodyParameter) parameter;
141+
if(bp.getSchema() != null && bp.getSchema() instanceof RefModel) {
142+
RefModel ref = (RefModel)bp.getSchema();
143+
if(ref.get$ref().startsWith("http")) {
144+
LOGGER.debug("added reference to " + ref.get$ref());
145+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, bp, "ref"));
146+
}
147+
}
148+
}
149+
else if(parameter instanceof RefParameter) {
150+
RefParameter ref = (RefParameter) parameter;
151+
LOGGER.debug("added reference to " + ref.get$ref());
152+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, operation, "inline"));
153+
}
154+
}
155+
}
156+
if(operation.getResponses() != null) {
157+
for(String responseCode : operation.getResponses().keySet()) {
158+
Response response = operation.getResponses().get(responseCode);
159+
if(response.getSchema() != null) {
160+
Property schema = response.getSchema();
161+
if(schema instanceof RefProperty) {
162+
RefProperty ref = (RefProperty) schema;
163+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
164+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, response, "ref"));
165+
}
166+
}
167+
}
168+
}
169+
}
170+
}
171+
}
172+
}
173+
174+
public void detectModelRefs() {
175+
Map<String, Model> models = swagger.getDefinitions();
176+
if(models != null) {
177+
for(String modelName : models.keySet()) {
178+
LOGGER.debug("looking at " + modelName);
179+
Model model = models.get(modelName);
180+
if(model instanceof RefModel) {
181+
RefModel ref = (RefModel) model;
182+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
183+
LOGGER.debug("added reference to " + ref.get$ref());
184+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, swagger.getDefinitions(), "ref"));
185+
}
186+
}
187+
else if(model instanceof ArrayModel) {
188+
ArrayModel arrayModel = (ArrayModel) model;
189+
if(arrayModel.getItems() != null && arrayModel.getItems() instanceof RefProperty) {
190+
RefProperty ref = (RefProperty)arrayModel.getItems();
191+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
192+
LOGGER.debug("added reference to " + ref.get$ref());
193+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, swagger.getDefinitions(), "ref"));
194+
}
195+
}
196+
}
197+
else if(model instanceof ModelImpl) {
198+
ModelImpl impl = (ModelImpl) model;
199+
Map<String, Property> properties = impl.getProperties();
200+
for(String propertyName : properties.keySet()) {
201+
Property property = properties.get(propertyName);
202+
if(property instanceof RefProperty) {
203+
RefProperty ref = (RefProperty)property;
204+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
205+
LOGGER.debug("added reference to " + ref.get$ref());
206+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, impl, "ref"));
207+
}
208+
}
209+
else if(property instanceof ArrayProperty) {
210+
ArrayProperty arrayProperty = (ArrayProperty) property;
211+
if(arrayProperty.getItems() != null && arrayProperty.getItems() instanceof RefProperty) {
212+
RefProperty ref = (RefProperty)arrayProperty.getItems();
213+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
214+
LOGGER.debug("added reference to " + ref.get$ref());
215+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, arrayProperty, "ref"));
216+
}
217+
}
218+
}
219+
else if(property instanceof MapProperty) {
220+
MapProperty mp = (MapProperty) property;
221+
if(mp.getAdditionalProperties() != null && mp.getAdditionalProperties() instanceof RefProperty) {
222+
RefProperty ref = (RefProperty)mp.getAdditionalProperties();
223+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
224+
LOGGER.debug("added reference to " + ref.get$ref());
225+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, mp, "ref"));
226+
}
227+
}
228+
}
229+
}
230+
}
231+
}
232+
}
233+
}
234+
235+
static class ResolutionContext {
236+
private Object object, parent;
237+
private String scope;
238+
239+
public ResolutionContext(Object object, Object parent, String scope) {
240+
this.object = object;
241+
this.parent = parent;
242+
this.scope = scope;
243+
}
244+
}
245+
}

0 commit comments

Comments
 (0)