2020public class SwaggerResolver {
2121 Logger LOGGER = LoggerFactory .getLogger (SwaggerResolver .class );
2222 protected Swagger swagger ;
23- protected Map <String , ResolutionContext > resolutionMap = new HashMap <String , ResolutionContext >();
23+ protected Map <String , List < ResolutionContext >> resolutionMap = new HashMap <String , List < ResolutionContext > >();
2424
2525 protected ResolverOptions opts ;
2626 public SwaggerResolver (){}
@@ -57,70 +57,71 @@ public void applyResolutions(List<AuthorizationValue> auths) {
5757 objectList = new ArrayList <Object >();
5858 hostToObjectMap .put (host , objectList );
5959 }
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 );
60+ List <ResolutionContext > contexts = resolutionMap .get (path );
61+ for (ResolutionContext ctx : contexts ) {
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+ }
9181 }
9282 }
93- else if (target instanceof Parameter ) {
94- if (mapping instanceof RefModel ) {
83+ if (location != null ) {
84+ // convert the node to the proper type
85+ if (mapping instanceof Property ) {
9586 Model model = Json .mapper ().convertValue (location , Model .class );
96- RefModel ref = (RefModel ) mapping ;
97- ref .set$ref (locationName );
98- swagger .addDefinition (locationName , model );
87+ if (mapping instanceof RefProperty ) {
88+ RefProperty ref = (RefProperty ) mapping ;
89+ ref .set$ref (locationName );
90+ swagger .addDefinition (locationName , model );
91+ }
9992 }
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 );
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+ }
114115 }
116+ position += 1 ;
115117 }
116- position += 1 ;
117118 }
118119 }
119120 }
120- }
121- catch ( Exception e ) {
122- // failed to get it
123- e . printStackTrace ();
121+ catch ( Exception e ) {
122+ // failed to get it
123+ e . printStackTrace ();
124+ }
124125 }
125126 }
126127 }
@@ -140,16 +141,29 @@ public void detectOperationRefs() {
140141 BodyParameter bp = (BodyParameter ) parameter ;
141142 if (bp .getSchema () != null && bp .getSchema () instanceof RefModel ) {
142143 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" ));
144+ String key = ref .get$ref ();
145+ if (key .startsWith ("http" )) {
146+ LOGGER .debug ("added reference to " + key );
147+ List <ResolutionContext > m = resolutionMap .get (key );
148+ if (m == null ) {
149+ m = new ArrayList <ResolutionContext >();
150+ }
151+ m .add (new ResolutionContext (ref , bp , "ref" ));
152+ resolutionMap .put (key , m );
146153 }
147154 }
148155 }
149156 else if (parameter instanceof RefParameter ) {
150157 RefParameter ref = (RefParameter ) parameter ;
151- LOGGER .debug ("added reference to " + ref .get$ref ());
152- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , operation , "inline" ));
158+ String key = ref .get$ref ();
159+ LOGGER .debug ("added reference to " + ref );
160+
161+ List <ResolutionContext > m = resolutionMap .get (key );
162+ if (m == null ) {
163+ m = new ArrayList <ResolutionContext >();
164+ }
165+ m .add (new ResolutionContext (ref , operation , "inline" ));
166+ resolutionMap .put (key , m );
153167 }
154168 }
155169 }
@@ -160,8 +174,15 @@ else if(parameter instanceof RefParameter) {
160174 Property schema = response .getSchema ();
161175 if (schema instanceof RefProperty ) {
162176 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" ));
177+ String key = ref .get$ref ();
178+
179+ if (key != null && key .startsWith ("http" )) {
180+ List <ResolutionContext > m = resolutionMap .get (key );
181+ if (m == null ) {
182+ m = new ArrayList <ResolutionContext >();
183+ }
184+ m .add (new ResolutionContext (ref , response , "ref" ));
185+ resolutionMap .put (key , m );
165186 }
166187 }
167188 }
@@ -179,18 +200,30 @@ public void detectModelRefs() {
179200 Model model = models .get (modelName );
180201 if (model instanceof RefModel ) {
181202 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" ));
203+ String key = ref .get$ref ();
204+ if (key != null && key .startsWith ("http" )) {
205+ LOGGER .debug ("added reference to " + key );
206+ List <ResolutionContext > m = resolutionMap .get (key );
207+ if (m == null ) {
208+ m = new ArrayList <ResolutionContext >();
209+ }
210+ m .add (new ResolutionContext (ref , swagger .getDefinitions (), "ref" ));
211+ resolutionMap .put (key , m );
185212 }
186213 }
187214 else if (model instanceof ArrayModel ) {
188215 ArrayModel arrayModel = (ArrayModel ) model ;
189216 if (arrayModel .getItems () != null && arrayModel .getItems () instanceof RefProperty ) {
190217 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" ));
218+ String key = ref .get$ref ();
219+ if (key != null && key .startsWith ("http" )) {
220+ LOGGER .debug ("added reference to " + key );
221+ List <ResolutionContext > m = resolutionMap .get (key );
222+ if (m == null ) {
223+ m = new ArrayList <ResolutionContext >();
224+ }
225+ m .add (new ResolutionContext (ref , swagger .getDefinitions (), "ref" ));
226+ resolutionMap .put (key , m );
194227 }
195228 }
196229 }
@@ -202,28 +235,46 @@ else if(model instanceof ModelImpl) {
202235 Property property = properties .get (propertyName );
203236 if (property instanceof RefProperty ) {
204237 RefProperty ref = (RefProperty )property ;
205- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
206- LOGGER .debug ("added reference to " + ref .get$ref ());
207- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , impl , "ref" ));
238+ String key = ref .get$ref ();
239+ if (key != null && key .startsWith ("http" )) {
240+ LOGGER .debug ("added reference to " + key );
241+ List <ResolutionContext > m = resolutionMap .get (key );
242+ if (m == null ) {
243+ m = new ArrayList <ResolutionContext >();
244+ }
245+ m .add (new ResolutionContext (ref , impl , "ref" ));
246+ resolutionMap .put (key , m );
208247 }
209248 }
210249 else if (property instanceof ArrayProperty ) {
211250 ArrayProperty arrayProperty = (ArrayProperty ) property ;
212251 if (arrayProperty .getItems () != null && arrayProperty .getItems () instanceof RefProperty ) {
213252 RefProperty ref = (RefProperty )arrayProperty .getItems ();
214- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
215- LOGGER .debug ("added reference to " + ref .get$ref ());
216- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , arrayProperty , "ref" ));
253+ String key = ref .get$ref ();
254+ if (key != null && key .startsWith ("http" )) {
255+ LOGGER .debug ("added reference to " + key );
256+ List <ResolutionContext > m = resolutionMap .get (key );
257+ if (m == null ) {
258+ m = new ArrayList <ResolutionContext >();
259+ }
260+ m .add (new ResolutionContext (ref , arrayProperty , "ref" ));
261+ resolutionMap .put (key , m );
217262 }
218263 }
219264 }
220265 else if (property instanceof MapProperty ) {
221266 MapProperty mp = (MapProperty ) property ;
222267 if (mp .getAdditionalProperties () != null && mp .getAdditionalProperties () instanceof RefProperty ) {
223268 RefProperty ref = (RefProperty )mp .getAdditionalProperties ();
224- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
225- LOGGER .debug ("added reference to " + ref .get$ref ());
226- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , mp , "ref" ));
269+ String key = ref .get$ref ();
270+ if (key != null && key .startsWith ("http" )) {
271+ LOGGER .debug ("added reference to " + key );
272+ List <ResolutionContext > m = resolutionMap .get (key );
273+ if (m == null ) {
274+ m = new ArrayList <ResolutionContext >();
275+ }
276+ m .add (new ResolutionContext (ref , mp , "ref" ));
277+ resolutionMap .put (key , m );
227278 }
228279 }
229280 }
0 commit comments