44
55import com .wordnik .swagger .util .Json ;
66import com .wordnik .swagger .models .*;
7+ import com .wordnik .swagger .models .parameters .*;
78import com .wordnik .swagger .models .properties .*;
89import com .wordnik .swagger .models .auth .AuthorizationValue ;
910
1819
1920public class SwaggerResolver {
2021 Logger LOGGER = LoggerFactory .getLogger (SwaggerResolver .class );
22+ protected Swagger swagger ;
23+ protected Map <String , ResolutionContext > resolutionMap = new HashMap <String , ResolutionContext >();
2124
2225 protected ResolverOptions opts ;
2326 public SwaggerResolver (){}
@@ -28,30 +31,23 @@ public Swagger resolve(Swagger swagger, List<AuthorizationValue> auths) {
2831 if (swagger == null )
2932 return null ;
3033
31- Map < String , Object > toResolve = new HashMap < String , Object >() ;
34+ this . swagger = swagger ;
3235
33- detectModelRefs (swagger , toResolve );
34-
35- applyResolutions (swagger , toResolve , auths );
36- // JsonNode node = Json.mapper().convertValue(swagger, JsonNode.class);
37-
38- // // models
39- // JsonNode definitions = node.get("definitions");
40- // if(definitions != null && definitions instanceof ObjectNode) {
41- // ObjectNode on = (ObjectNode)definitions;
42-
43- // }
36+ // models
37+ detectModelRefs ();
4438
4539 // operations
40+ detectOperationRefs ();
4641
47- return swagger ;
42+ applyResolutions (auths );
43+ return this .swagger ;
4844 }
4945
50- public void applyResolutions (Swagger swagger , Map < String , Object > toResolve , List <AuthorizationValue > auths ) {
46+ public void applyResolutions (List <AuthorizationValue > auths ) {
5147 // hosts to call
5248 Map <String , List <Object >> hostToObjectMap = new HashMap <String , List <Object >>();
5349
54- for (String path : toResolve .keySet ()) {
50+ for (String path : resolutionMap .keySet ()) {
5551 String [] parts = path .split ("#" );
5652 if (parts .length == 2 ) {
5753 String host = parts [0 ];
@@ -61,8 +57,10 @@ public void applyResolutions(Swagger swagger, Map<String, Object> toResolve, Lis
6157 objectList = new ArrayList <Object >();
6258 hostToObjectMap .put (host , objectList );
6359 }
64- Object mapping = toResolve .get (path );
65-
60+ ResolutionContext ctx = resolutionMap .get (path );
61+
62+ Object mapping = ctx .object ;
63+ Object target = ctx .parent ;
6664 try {
6765 String contents = new RemoteUrl ().urlToString (host , auths );
6866 JsonNode location = null ;
@@ -88,6 +86,14 @@ public void applyResolutions(Swagger swagger, Map<String, Object> toResolve, Lis
8886 swagger .addDefinition (locationName , model );
8987 }
9088 }
89+ else if (target instanceof Parameter ) {
90+ if (mapping instanceof RefModel ) {
91+ Model model = Json .mapper ().convertValue (location , Model .class );
92+ RefModel ref = (RefModel ) mapping ;
93+ ref .set$ref (locationName );
94+ swagger .addDefinition (locationName , model );
95+ }
96+ }
9197 }
9298 }
9399 catch (Exception e ) {
@@ -96,10 +102,34 @@ public void applyResolutions(Swagger swagger, Map<String, Object> toResolve, Lis
96102 }
97103 }
98104 }
99- // Json.prettyPrint(toResolve );
105+ // Json.prettyPrint(resolutionMap );
100106 }
101107
102- public void detectModelRefs (Swagger swagger , Map <String , Object > toResolve ) {
108+ public void detectOperationRefs () {
109+ Map <String , Path > paths = swagger .getPaths ();
110+ if (paths == null ) return ;
111+
112+ for (String pathName : paths .keySet ()) {
113+ Path path = paths .get (pathName );
114+ List <Operation > operations = path .getOperations ();
115+ for (Operation operation : operations ) {
116+ for (Parameter parameter : operation .getParameters ()) {
117+ if (parameter instanceof BodyParameter ) {
118+ BodyParameter bp = (BodyParameter ) parameter ;
119+ if (bp .getSchema () != null && bp .getSchema () instanceof RefModel ) {
120+ RefModel ref = (RefModel )bp .getSchema ();
121+ if (ref .get$ref ().startsWith ("http" )) {
122+ LOGGER .debug ("added reference to " + ref .get$ref ());
123+ resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , bp , "ref" ));
124+ }
125+ }
126+ }
127+ }
128+ }
129+ }
130+ }
131+
132+ public void detectModelRefs () {
103133 Map <String , Model > models = swagger .getDefinitions ();
104134 if (models != null ) {
105135 for (String modelName : models .keySet ()) {
@@ -109,7 +139,7 @@ public void detectModelRefs(Swagger swagger, Map<String, Object> toResolve) {
109139 RefModel ref = (RefModel ) model ;
110140 if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
111141 LOGGER .debug ("added reference to " + ref .get$ref ());
112- toResolve .put (ref .get$ref (), ref );
142+ resolutionMap .put (ref .get$ref (), new ResolutionContext ( ref , swagger . getDefinitions (), "ref" ) );
113143 }
114144 }
115145 else if (model instanceof ArrayModel ) {
@@ -118,7 +148,7 @@ else if(model instanceof ArrayModel) {
118148 RefProperty ref = (RefProperty )arrayModel .getItems ();
119149 if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
120150 LOGGER .debug ("added reference to " + ref .get$ref ());
121- toResolve .put (ref .get$ref (), ref );
151+ resolutionMap .put (ref .get$ref (), new ResolutionContext ( ref , swagger . getDefinitions (), "ref" ) );
122152 }
123153 }
124154 }
@@ -131,7 +161,7 @@ else if(model instanceof ModelImpl) {
131161 RefProperty ref = (RefProperty )property ;
132162 if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
133163 LOGGER .debug ("added reference to " + ref .get$ref ());
134- toResolve .put (ref .get$ref (), ref );
164+ resolutionMap .put (ref .get$ref (), new ResolutionContext ( ref , impl , "ref" ) );
135165 }
136166 }
137167 else if (property instanceof ArrayProperty ) {
@@ -140,7 +170,7 @@ else if(property instanceof ArrayProperty) {
140170 RefProperty ref = (RefProperty )arrayProperty .getItems ();
141171 if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
142172 LOGGER .debug ("added reference to " + ref .get$ref ());
143- toResolve .put (ref .get$ref (), ref );
173+ resolutionMap .put (ref .get$ref (), new ResolutionContext ( ref , arrayProperty , "ref" ) );
144174 }
145175 }
146176 }
@@ -150,7 +180,7 @@ else if(property instanceof MapProperty) {
150180 RefProperty ref = (RefProperty )mp .getAdditionalProperties ();
151181 if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
152182 LOGGER .debug ("added reference to " + ref .get$ref ());
153- toResolve .put (ref .get$ref (), ref );
183+ resolutionMap .put (ref .get$ref (), new ResolutionContext ( ref , mp , "ref" ) );
154184 }
155185 }
156186 }
@@ -159,4 +189,15 @@ else if(property instanceof MapProperty) {
159189 }
160190 }
161191 }
192+
193+ static class ResolutionContext {
194+ private Object object , parent ;
195+ private String scope ;
196+
197+ public ResolutionContext (Object object , Object parent , String scope ) {
198+ this .object = object ;
199+ this .parent = parent ;
200+ this .scope = scope ;
201+ }
202+ }
162203}
0 commit comments