Skip to content

Commit 600d0df

Browse files
authored
Stable hash for determining if input specification changed (#24645)
* Stable hash for determining if input specification changed * Handle unparsable input specification
1 parent 7c090ab commit 600d0df

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

  • modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin
  • samples/client/petstore/java/resttemplate-springBoot4-jackson3

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717

1818
package org.openapitools.codegen.plugin;
1919

20+
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
2022
import com.google.common.hash.Hashing;
2123
import com.google.common.io.Files;
2224
import io.swagger.parser.OpenAPIParser;
2325
import io.swagger.v3.core.util.Json;
26+
import io.swagger.v3.core.util.Json31;
2427
import io.swagger.v3.core.util.Yaml;
28+
import io.swagger.v3.oas.models.OpenAPI;
29+
import io.swagger.v3.oas.models.SpecVersion;
2530
import io.swagger.v3.parser.OpenAPIResolver;
2631
import io.swagger.v3.parser.OpenAPIV3Parser;
2732
import io.swagger.v3.parser.core.models.AuthorizationValue;
@@ -1155,10 +1160,22 @@ private String calculateInputSpecHash(String inputSpec) {
11551160
final URL remoteUrl = inputSpecRemoteUrl();
11561161
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
11571162

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+
}
11621179
}
11631180

11641181
/**

samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
<properties>
270270
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
271271

272-
<spring-web-version>7.0.8</spring-web-version>
272+
<spring-web-version>7.0.5</spring-web-version>
273273
<jackson-version>3.1.5</jackson-version>
274274
<jakarta-annotation-version>3.0.0</jakarta-annotation-version>
275275

0 commit comments

Comments
 (0)