diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index bb06c5de5ad5..084fab18b5c4 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -17,11 +17,16 @@ package org.openapitools.codegen.plugin; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.hash.Hashing; import com.google.common.io.Files; import io.swagger.parser.OpenAPIParser; import io.swagger.v3.core.util.Json; +import io.swagger.v3.core.util.Json31; import io.swagger.v3.core.util.Yaml; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.SpecVersion; import io.swagger.v3.parser.OpenAPIResolver; import io.swagger.v3.parser.OpenAPIV3Parser; import io.swagger.v3.parser.core.models.AuthorizationValue; @@ -1155,10 +1160,22 @@ private String calculateInputSpecHash(String inputSpec) { final URL remoteUrl = inputSpecRemoteUrl(); final List authorizationValues = AuthParser.parse(this.auth); - return Hashing.sha256().hashBytes( - new OpenAPIParser().readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), authorizationValues, parseOptions) - .getOpenAPI().toString().getBytes(StandardCharsets.UTF_8) - ).toString(); + final OpenAPI spec = new OpenAPIParser() + .readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), authorizationValues, parseOptions) + .getOpenAPI(); + + // If the specification is not parsable, a unique string is returned so the subsequent steps are not skipped. + // It is up to them to parse it again and deal with the error. + if (spec == null) + return "INVALID-HASH-" + System.currentTimeMillis(); + + final ObjectMapper mapper = spec.getSpecVersion() == SpecVersion.V30 ? Json.mapper() : Json31.mapper(); + + try { + return Hashing.sha256().hashBytes(mapper.writeValueAsBytes(spec)).toString(); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } } /** diff --git a/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml b/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml index b5c5141bdd3d..859141904ec9 100644 --- a/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml +++ b/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml @@ -269,7 +269,7 @@ UTF-8 - 7.0.8 + 7.0.5 3.1.5 3.0.0