|
23 | 23 | import com.fasterxml.jackson.databind.ObjectMapper; |
24 | 24 | import io.swagger.v3.core.util.Json; |
25 | 25 | import io.swagger.v3.oas.models.*; |
| 26 | +import io.swagger.v3.oas.models.PathItem.HttpMethod; |
26 | 27 | import io.swagger.v3.oas.models.callbacks.Callback; |
27 | 28 | import io.swagger.v3.oas.models.media.*; |
28 | 29 | import io.swagger.v3.oas.models.parameters.Parameter; |
@@ -107,52 +108,63 @@ private void flattenPaths() { |
107 | 108 |
|
108 | 109 | for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) { |
109 | 110 | PathItem path = pathsEntry.getValue(); |
110 | | - List<Operation> operations = new ArrayList<>(path.readOperations()); |
| 111 | + Map<HttpMethod, Operation> operationsMap = new LinkedHashMap<>(path.readOperationsMap()); |
111 | 112 |
|
112 | 113 | // use path name (e.g. /foo/bar) and HTTP verb to come up with a name |
113 | 114 | // in case operationId is not defined later in other methods |
114 | 115 | String pathname = pathsEntry.getKey(); |
115 | | - String name = pathname; |
116 | | - if (path.getDelete() != null) { |
117 | | - name = pathname + "_delete"; |
118 | | - } else if (path.getGet() != null) { |
119 | | - name = pathname + "_get"; |
120 | | - } else if (path.getHead() != null) { |
121 | | - name = pathname + "_head"; |
122 | | - } else if (path.getOptions() != null) { |
123 | | - name = pathname + "_options"; |
124 | | - } else if (path.getPatch() != null) { |
125 | | - name = pathname + "_patch"; |
126 | | - } else if (path.getPost() != null) { |
127 | | - name = pathname + "_post"; |
128 | | - } else if (path.getPut() != null) { |
129 | | - name = pathname + "_put"; |
130 | | - } else if (path.getTrace() != null) { |
131 | | - name = pathname + "_trace"; |
132 | | - } else { |
133 | | - // no HTTP verb defined? |
134 | | - //throw new RuntimeException("No HTTP verb found/detected in the inline model resolver"); |
135 | | - } |
136 | 116 |
|
137 | 117 | // Include callback operation as well |
138 | | - for (Operation operation : path.readOperations()) { |
| 118 | + for (Map.Entry<HttpMethod, Operation> operationEntry : new LinkedHashMap<>(path.readOperationsMap()).entrySet()) { |
| 119 | + Operation operation = operationEntry.getValue(); |
139 | 120 | Map<String, Callback> callbacks = operation.getCallbacks(); |
140 | 121 | if (callbacks != null) { |
141 | | - operations.addAll(callbacks.values().stream() |
142 | | - .flatMap(callback -> callback.values().stream()) |
143 | | - .flatMap(pathItem -> pathItem.readOperations().stream()) |
144 | | - .collect(Collectors.toList())); |
| 122 | + for (Map.Entry<String, Callback> callbackEntry : callbacks.entrySet()) { |
| 123 | + Callback callback = callbackEntry.getValue(); |
| 124 | + for (Map.Entry<String, PathItem> pathItemEntry : callback.entrySet()) { |
| 125 | + PathItem pathItem = pathItemEntry.getValue(); |
| 126 | + operationsMap.putAll(pathItem.readOperationsMap()); |
| 127 | + } |
| 128 | + } |
145 | 129 | } |
146 | 130 | } |
147 | 131 |
|
148 | | - for (Operation operation : operations) { |
149 | | - flattenRequestBody(name, operation); |
150 | | - flattenParameters(name, operation); |
151 | | - flattenResponses(name, operation); |
| 132 | + for (Map.Entry<HttpMethod, Operation> operationEntry : operationsMap.entrySet()) { |
| 133 | + Operation operation = operationEntry.getValue(); |
| 134 | + String inlineSchemaName = this.getInlineSchemaName(operationEntry.getKey(), pathname); |
| 135 | + flattenRequestBody(inlineSchemaName, operation); |
| 136 | + flattenParameters(inlineSchemaName, operation); |
| 137 | + flattenResponses(inlineSchemaName, operation); |
152 | 138 | } |
153 | 139 | } |
154 | 140 | } |
155 | 141 |
|
| 142 | + private String getInlineSchemaName(HttpMethod httpVerb, String pathname) { |
| 143 | + String name = pathname; |
| 144 | + if (httpVerb.equals(HttpMethod.DELETE)) { |
| 145 | + name += "_delete"; |
| 146 | + } else if (httpVerb.equals(HttpMethod.GET)) { |
| 147 | + name += "_get"; |
| 148 | + } else if (httpVerb.equals(HttpMethod.HEAD)) { |
| 149 | + name += "_head"; |
| 150 | + } else if (httpVerb.equals(HttpMethod.OPTIONS)) { |
| 151 | + name += "_options"; |
| 152 | + } else if (httpVerb.equals(HttpMethod.PATCH)) { |
| 153 | + name += "_patch"; |
| 154 | + } else if (httpVerb.equals(HttpMethod.POST)) { |
| 155 | + name += "_post"; |
| 156 | + } else if (httpVerb.equals(HttpMethod.PUT)) { |
| 157 | + name += "_put"; |
| 158 | + } else if (httpVerb.equals(HttpMethod.TRACE)) { |
| 159 | + name += "_trace"; |
| 160 | + } else { |
| 161 | + // no HTTP verb defined? |
| 162 | + // throw new RuntimeException("No HTTP verb found/detected in the inline model |
| 163 | + // resolver"); |
| 164 | + } |
| 165 | + return name; |
| 166 | + } |
| 167 | + |
156 | 168 | /** |
157 | 169 | * Return false if model can be represented by primitives e.g. string, object |
158 | 170 | * without properties, array or map of other model (model contanier), etc. |
|
0 commit comments