|
17 | 17 |
|
18 | 18 | package org.openapitools.codegen.plugin; |
19 | 19 |
|
| 20 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
20 | 22 | import com.google.common.hash.Hashing; |
21 | 23 | import com.google.common.io.Files; |
22 | 24 | import io.swagger.parser.OpenAPIParser; |
23 | 25 | import io.swagger.v3.core.util.Json; |
| 26 | +import io.swagger.v3.core.util.Json31; |
24 | 27 | import io.swagger.v3.core.util.Yaml; |
| 28 | +import io.swagger.v3.oas.models.OpenAPI; |
| 29 | +import io.swagger.v3.oas.models.SpecVersion; |
25 | 30 | import io.swagger.v3.parser.OpenAPIResolver; |
26 | 31 | import io.swagger.v3.parser.OpenAPIV3Parser; |
27 | 32 | import io.swagger.v3.parser.core.models.AuthorizationValue; |
@@ -1155,10 +1160,22 @@ private String calculateInputSpecHash(String inputSpec) { |
1155 | 1160 | final URL remoteUrl = inputSpecRemoteUrl(); |
1156 | 1161 | final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth); |
1157 | 1162 |
|
1158 | | - return Hashing.sha256().hashBytes( |
1159 | | - new OpenAPIParser().readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), authorizationValues, parseOptions) |
1160 | | - .getOpenAPI().toString().getBytes(StandardCharsets.UTF_8) |
1161 | | - ).toString(); |
| 1163 | + final OpenAPI spec = new OpenAPIParser() |
| 1164 | + .readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), authorizationValues, parseOptions) |
| 1165 | + .getOpenAPI(); |
| 1166 | + |
| 1167 | + // If the specification is not parsable, a unique string is returned so the subsequent steps are not skipped. |
| 1168 | + // It is up to them to parse it again and deal with the error. |
| 1169 | + if (spec == null) |
| 1170 | + return "INVALID-HASH-" + System.currentTimeMillis(); |
| 1171 | + |
| 1172 | + final ObjectMapper mapper = spec.getSpecVersion() == SpecVersion.V30 ? Json.mapper() : Json31.mapper(); |
| 1173 | + |
| 1174 | + try { |
| 1175 | + return Hashing.sha256().hashBytes(mapper.writeValueAsBytes(spec)).toString(); |
| 1176 | + } catch (JsonProcessingException e) { |
| 1177 | + throw new RuntimeException(e); |
| 1178 | + } |
1162 | 1179 | } |
1163 | 1180 |
|
1164 | 1181 | /** |
|
0 commit comments