Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading Smithy files from jars with urlencoded characters in paths #850

Merged
merged 9 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions modules/codegen/src/smithy4s/codegen/internals/ModelLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import software.amazon.smithy.model.loader.ModelManifestException
import java.io.File
import java.net.URLClassLoader
import scala.jdk.CollectionConverters._
import java.nio.file.FileSystems
import scala.util.Using
import java.nio.file.Files

private[codegen] object ModelLoader {

Expand All @@ -45,12 +48,25 @@ private[codegen] object ModelLoader {
val currentClassLoader = this.getClass().getClassLoader()
val deps = resolveDependencies(dependencies, localJars, repositories)

val modelsInJars = deps.flatMap { files =>
val manifestUrl =
ModelDiscovery.createSmithyJarManifestUrl(files.getAbsolutePath())
try { ModelDiscovery.findModels(manifestUrl).asScala }
catch {
case _: ModelManifestException => Seq.empty
val modelsInJars = deps.flatMap { file =>
Using.resource(
// Note: On JDK13+, the second parameter is redundant.
FileSystems.newFileSystem(file.toPath(), null)
) { jarFS =>
val p = jarFS.getPath("META-INF", "smithy", "manifest")

// model discovery would throw if we tried to pass a non-existent path
if (!Files.exists(p)) Nil
else {
try ModelDiscovery.findModels(p.toUri().toURL()).asScala.toList
catch {
case e: ModelManifestException =>
System.err.println(
s"Unexpected exception while loading model from $file, skipping: $e"
)
Nil
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2021-2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 smithy4s.codegen.internals
import munit._
import software.amazon.smithy.model.shapes.ShapeId
import java.util.stream.Collectors
import software.amazon.smithy.model.Model
import scala.jdk.CollectionConverters._

class ModelLoaderSpec extends FunSuite {
private def doLoad(dependencies: List[String], repositories: List[String]) =
ModelLoader
.load(
specs = Set.empty,
dependencies,
repositories,
transformers = Nil,
discoverModels = false,
localJars = Nil
)
._2

private def allNamespaces(model: Model): Set[String] =
model
.shapes()
.collect(Collectors.toList())
.asScala
.toList
.map(_.getId().getNamespace())
.toSet

test("ModelLoader can load a dependency from s01") {
val model = doLoad(
dependencies =
List("org.polyvariant:test-library-core_2.13:0.0.1-SNAPSHOT"),
repositories =
List("https://s01.oss.sonatype.org/content/repositories/snapshots")
)

assertEquals(
allNamespaces(model),
Set("smithy.api", "smithy4s.meta", "testlibrary")
)

model.expectShape(ShapeId.from("testlibrary#MyString"))
}

test("ModelLoader can load a dependency from s01 if it has a + in the name") {
kubukoz marked this conversation as resolved.
Show resolved Hide resolved
val model = doLoad(
dependencies =
List("org.polyvariant:test-library-core_2.13:0.0.1+123-SNAPSHOT"),
repositories =
List("https://s01.oss.sonatype.org/content/repositories/snapshots")
)

assertEquals(
allNamespaces(model),
Set("smithy.api", "smithy4s.meta", "testlibrary")
)

model.expectShape(ShapeId.from("testlibrary#MyString"))
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 smithy4s.codegen.internals

final class ScaladocSpec extends munit.FunSuite {
Expand Down
16 changes: 16 additions & 0 deletions modules/core/test/src/smithy4s/schema/DefaultValueSpec.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 smithy4s
package schema

Expand Down
16 changes: 16 additions & 0 deletions modules/dynamic/test/src-jvm/smithy4s/dynamic/ResourceSpec.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 smithy4s
package dynamic

Expand Down
16 changes: 16 additions & 0 deletions modules/dynamic/test/src-jvm/smithy4s/dynamic/package.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 smithy4s

import smithy4s.dynamic.DummyIO._
Expand Down