-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip unrecognized JSON files when loading models
This commit updates the model loading process so that JSON files that are not objects or that don't contain a top-level "smithy" key are skipped. This can be useful when loading directories that contain Smithy models mixed with other JSON files. JAR files that contain Smithy models are not permitted to refer to unrecognized JSON files that lack a valid "smithy" version key/value pair. This commit also only creates InputStreams for files that Smithy can actually load or attempt to load. Smithy would previously open an InputStream for all recursive files in a directory even if it was unable to load the file based on the filename. It now only creates an InputStream when the file has a .json, .smithy, or no file extension. Closes #1841
- Loading branch information
Showing
12 changed files
with
194 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
smithy-model/src/test/java/software/amazon/smithy/model/JarUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.model; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.jar.Attributes; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarOutputStream; | ||
import java.util.jar.Manifest; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
@SmithyInternalApi | ||
public final class JarUtils { | ||
/** | ||
* Creates a JAR in a temp directory on demand for test cases based on a directory. | ||
* | ||
* <p>This method is preferred over embedding JARs directly as resources when possible, because generated JARs | ||
* don't need to be manually recreated if their contents need to change, and we don't need to commit blobs to VCS. | ||
* | ||
* <p>TODO: migrate other test cases to use this. | ||
* | ||
* @param source Where the files for the JAR are stored, including the required "META-INF/MANIFEST.MF" file. | ||
* @return Returns the path to the temporary JAR file. | ||
*/ | ||
public static Path createJarFromDir(Path source) { | ||
try { | ||
Path target = Files.createTempFile("temp-jar", ".jar"); | ||
|
||
Path relativeManifestLocation = Paths.get("META-INF").resolve("MANIFEST.MF"); | ||
Manifest manifest; | ||
|
||
// Requires a manifest to be provided. | ||
Path manifestLocation = target.resolve(relativeManifestLocation); | ||
if (Files.isRegularFile(manifestLocation)) { | ||
manifest = new Manifest(Files.newInputStream(manifestLocation)); | ||
} else { | ||
manifest = new Manifest(); | ||
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); | ||
} | ||
|
||
try (JarOutputStream stream = new JarOutputStream(Files.newOutputStream(target), manifest)) { | ||
Files.walkFileTree(source, new SimpleFileVisitor<Path>() { | ||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
Path relative = source.relativize(file); | ||
// The manifest is added through the constructor. | ||
if (!relative.equals(relativeManifestLocation)) { | ||
JarEntry entry = new JarEntry(relative.toString()); | ||
entry.setTime(file.toFile().lastModified()); | ||
stream.putNextEntry(entry); | ||
Files.copy(file, stream); | ||
} | ||
return FileVisitResult.CONTINUE; | ||
} | ||
}); | ||
} | ||
|
||
return target; | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...urces/software/amazon/smithy/model/loader/assembler-fail-invalid-jar/META-INF/MANIFEST.MF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Manifest-Version: 1.0 | ||
Created-By: 11.0.6 (Amazon.com Inc.) |
2 changes: 2 additions & 0 deletions
2
.../amazon/smithy/model/loader/assembler-fail-invalid-jar/META-INF/smithy/invalid-array.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
[] |
4 changes: 4 additions & 0 deletions
4
...s/software/amazon/smithy/model/loader/assembler-fail-invalid-jar/META-INF/smithy/manifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This is loaded first and succeeds. | ||
valid.smithy | ||
# This fails because JARs cannot explicitly refer to unrecognized models files or JSON files. | ||
invalid-array.json |
5 changes: 5 additions & 0 deletions
5
...ftware/amazon/smithy/model/loader/assembler-fail-invalid-jar/META-INF/smithy/valid.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
string MyString |
5 changes: 5 additions & 0 deletions
5
...urces/software/amazon/smithy/model/loader/assembler-ignore-unrecognized-files/main.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$version: "2" | ||
|
||
namespace smithy.example | ||
|
||
string MyString |
1 change: 1 addition & 0 deletions
1
...esources/software/amazon/smithy/model/loader/assembler-ignore-unrecognized-files/test.ion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
...sources/software/amazon/smithy/model/loader/assembler-ignore-unrecognized-json/array.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[1, 2, 3] |
8 changes: 8 additions & 0 deletions
8
...ources/software/amazon/smithy/model/loader/assembler-ignore-unrecognized-json/smithy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"smithy": "2", | ||
"shapes": { | ||
"smithy.example#MyString": { | ||
"type": "string" | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
.../software/amazon/smithy/model/loader/assembler-ignore-unrecognized-json/unrecognized.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"foo": 1 | ||
} |