diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java index 3fbd352bfeb..e40ae9d50d6 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java @@ -124,7 +124,7 @@ public void execute(Arguments arguments, ClassLoader classLoader) { } private ValidatedResult buildModel(ClassLoader classLoader, List models, Arguments arguments) { - ModelAssembler assembler = Model.assembler(classLoader); + ModelAssembler assembler = CommandUtils.createModelAssembler(classLoader); CommandUtils.handleModelDiscovery(arguments, assembler, classLoader); CommandUtils.handleUnknownTraitsOption(arguments, assembler); models.forEach(assembler::addImport); diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java index 3e83d1b7955..1449edf2046 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java @@ -25,6 +25,7 @@ import software.amazon.smithy.cli.Arguments; import software.amazon.smithy.cli.CliError; import software.amazon.smithy.cli.SmithyCli; +import software.amazon.smithy.model.Model; import software.amazon.smithy.model.loader.ModelAssembler; final class CommandUtils { @@ -39,6 +40,10 @@ static void handleUnknownTraitsOption(Arguments arguments, ModelAssembler assemb } } + static ModelAssembler createModelAssembler(ClassLoader classLoader) { + return Model.assembler(classLoader).putProperty(ModelAssembler.DISABLE_JAR_CACHE, true); + } + static void handleModelDiscovery(Arguments arguments, ModelAssembler assembler, ClassLoader baseLoader) { if (arguments.has(SmithyCli.DISCOVER_CLASSPATH)) { discoverModelsWithClasspath(arguments, assembler); diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/DiffCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/DiffCommand.java index bbc6cf0e7b0..7acb37d3ae0 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/DiffCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/DiffCommand.java @@ -58,7 +58,7 @@ public void execute(Arguments arguments, ClassLoader classLoader) { List newModels = arguments.repeatedParameter("--new"); LOGGER.info(String.format("Setting 'new' Smithy models: %s", String.join(" ", newModels))); - ModelAssembler assembler = Model.assembler(classLoader); + ModelAssembler assembler = CommandUtils.createModelAssembler(classLoader); Model oldModel = loadModel("old", assembler, oldModels); assembler.reset(); Model newModel = loadModel("new", assembler, newModels); diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidateCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidateCommand.java index e314947032b..6bda03135d7 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidateCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidateCommand.java @@ -54,7 +54,7 @@ public void execute(Arguments arguments, ClassLoader classLoader) { List models = arguments.positionalArguments(); LOGGER.info(String.format("Validating Smithy model sources: %s", models)); - ModelAssembler assembler = Model.assembler(classLoader); + ModelAssembler assembler = CommandUtils.createModelAssembler(classLoader); CommandUtils.handleModelDiscovery(arguments, assembler, classLoader); CommandUtils.handleUnknownTraitsOption(arguments, assembler); diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/loader/JarModelLoader.java b/smithy-model/src/main/java/software/amazon/smithy/model/loader/JarModelLoader.java index 85322dd9e55..35035e77911 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/loader/JarModelLoader.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/loader/JarModelLoader.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLConnection; import java.util.function.Supplier; import java.util.logging.Logger; import software.amazon.smithy.utils.IoUtils; @@ -45,9 +46,17 @@ public boolean load(String filename, Supplier contentSupplier, LoaderVis LOGGER.fine(() -> "Loading Smithy model imports from JAR: " + manifestUrl); for (URL model : ModelDiscovery.findModels(manifestUrl)) { - try (InputStream is = model.openStream()) { - String contents = IoUtils.toUtf8String(is); - delegate.load(model.toExternalForm(), () -> contents, visitor); + try { + URLConnection connection = model.openConnection(); + + if (visitor.hasProperty(ModelAssembler.DISABLE_JAR_CACHE)) { + connection.setUseCaches(false); + } + + try (InputStream is = connection.getInputStream()) { + String contents = IoUtils.toUtf8String(is); + delegate.load(model.toExternalForm(), () -> contents, visitor); + } } catch (IOException e) { throw new ModelImportException( String.format("Error loading Smithy model from URL `%s`: %s", model, e.getMessage()), e); diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java b/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java index ce36a5f197d..665c31fa713 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java @@ -19,6 +19,7 @@ import java.io.InputStream; import java.io.UncheckedIOException; import java.net.URL; +import java.net.URLConnection; import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; @@ -61,7 +62,22 @@ * @see Model#assembler() */ public final class ModelAssembler { + /** + * Allow unknown traits rather than fail. + */ public static final String ALLOW_UNKNOWN_TRAITS = "assembler.allowUnknownTraits"; + + /** + * Sets {@link URLConnection#setUseCaches} to false. + * + *

When running in a build environment, using caches can cause exceptions + * like `java.util.zip.ZipException: ZipFile invalid LOC header (bad signature)` + * because a previously loaded JAR might change between builds. The + * "assembler.disableJarCache" setting should be set to true when embedding + * Smithy into an environment where this can occur. + */ + public static final String DISABLE_JAR_CACHE = "assembler.disableJarCache"; + private static final Logger LOGGER = Logger.getLogger(ModelAssembler.class.getName()); private static final ModelLoader DEFAULT_LOADER = ModelLoader.createDefaultLoader(); @@ -126,7 +142,6 @@ public ModelAssembler copy() { *

  • Models registered via {@link #addModel}
  • *
  • Shape registered via {@link #addModel}
  • *
  • Metadata registered via {@link #putMetadata}
  • - *
  • Custom properties set via {@link #putProperty}
  • * * *

    The state of {@link #disablePrelude} is reset such that the prelude @@ -142,7 +157,6 @@ public ModelAssembler reset() { validators.clear(); suppressions.clear(); documentNodes.clear(); - properties.clear(); disablePrelude = false; return this; }