-
Notifications
You must be signed in to change notification settings - Fork 94
More predictable handling of trait/validator classes model loader #1709
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dd01c5a
More deterministic model loader
kubukoz 7d72440
dunno
kubukoz 5e44d2d
Merge branch 'series/0.18' into modelloader-more-determinism
kubukoz 63ab0e3
Add test for transformations
kubukoz 92a3dcf
set scala version just in case
kubukoz 887e917
Add another test
kubukoz 135cf20
Add headers
kubukoz 260e0a6
Add changelog entry
kubukoz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
modules/codegen-plugin/src/sbt-test/codegen-plugin/transformation-trait-classes/build.sbt
This file contains hidden or 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,24 @@ | ||
| lazy val transformation = project | ||
| .settings( | ||
| scalaVersion := "2.12.20", | ||
| libraryDependencies ++= Seq( | ||
| "software.amazon.smithy" % "smithy-build" % "1.57.1", | ||
| "ch.epfl.scala" % "spec-traits" % "2.2.0-M2" | ||
| ) | ||
| ) | ||
|
|
||
| lazy val root = project | ||
| .in(file(".")) | ||
| .enablePlugins(Smithy4sCodegenPlugin) | ||
| .settings( | ||
| scalaVersion := "3.3.6", | ||
| libraryDependencies ++= Seq( | ||
| "ch.epfl.scala" % "spec-traits" % "2.2.0-M2" % Smithy4s, | ||
| "com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value | ||
| ), | ||
| Compile / smithy4sModelTransformers := List( | ||
| "my-transformation" | ||
| ), | ||
| Compile / smithy4sAllowedNamespaces := List("my.input"), | ||
| Compile / smithy4sAllDependenciesAsJars += (transformation / Compile / packageBin).value | ||
| ) |
9 changes: 9 additions & 0 deletions
9
...degen-plugin/src/sbt-test/codegen-plugin/transformation-trait-classes/project/plugins.sbt
This file contains hidden or 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,9 @@ | ||
| sys.props.get("plugin.version") match { | ||
| case Some(x) => | ||
| addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % x) | ||
| case _ => | ||
| sys.error( | ||
| """|The system property 'plugin.version' is not defined. | ||
| |Specify this property using the scriptedLaunchOpts -D.""".stripMargin | ||
| ) | ||
| } |
30 changes: 30 additions & 0 deletions
30
...plugin/src/sbt-test/codegen-plugin/transformation-trait-classes/src/main/scala/Main.scala
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Copyright 2021-2025 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. | ||
| */ | ||
|
|
||
| object Main extends App { | ||
| val hints = my.input.MyShape.schema.hints | ||
|
|
||
| require( | ||
| hints.has[smithy.api.Documentation], | ||
| s"Expected to have the Documentation trait, but it was missing: $hints" | ||
| ) | ||
|
|
||
| require( | ||
| hints.get[smithy.api.Documentation].get.value == "what's up doc", | ||
| s"Documentation trait mismatch: ${hints.get[smithy.api.Documentation].get.value}" | ||
| ) | ||
| println("all good: " + hints.all) | ||
| } |
6 changes: 6 additions & 0 deletions
6
...gin/src/sbt-test/codegen-plugin/transformation-trait-classes/src/main/smithy/input.smithy
This file contains hidden or 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,6 @@ | ||
| $version: "2" | ||
|
|
||
| namespace my.input | ||
|
|
||
| @traits#data | ||
| document MyShape |
4 changes: 4 additions & 0 deletions
4
modules/codegen-plugin/src/sbt-test/codegen-plugin/transformation-trait-classes/test
This file contains hidden or 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 @@ | ||
| # check if the app runs successfully. | ||
| # if it doesn't compile, it could be because the transformation wasn't applied. | ||
| # if the transformation throws, it's because of a bug like #336. | ||
| > run | ||
1 change: 1 addition & 0 deletions
1
...n/src/main/resources/META-INF/services/software.amazon.smithy.build.ProjectionTransformer
This file contains hidden or 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 @@ | ||
| MyTransformation |
59 changes: 59 additions & 0 deletions
59
...-plugin/transformation-trait-classes/transformation/src/main/scala/MyTransformation.scala
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * Copyright 2021-2025 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. | ||
| */ | ||
|
|
||
| import software.amazon.smithy.build.ProjectionTransformer | ||
| import software.amazon.smithy.build.TransformContext | ||
| import software.amazon.smithy.model.Model | ||
| import software.amazon.smithy.model.transform.ModelTransformer | ||
| import java.util.function.BiFunction | ||
| import software.amazon.smithy.model.traits.Trait | ||
| import software.amazon.smithy.model.shapes.Shape | ||
| import software.amazon.smithy.model.traits.DocumentationTrait | ||
| import software.amazon.smithy.model.shapes.ShapeId | ||
| import bsp.traits.DataTrait | ||
|
|
||
| class MyTransformation extends ProjectionTransformer { | ||
| def getName(): String = "my-transformation" | ||
|
|
||
| // Replace traits#jsonRPC with documentation | ||
| def transform(context: TransformContext): Model = { | ||
| // this would fail if the class wasn't present on the classpath. | ||
|
|
||
| // external shape - regression test for #336 | ||
| context | ||
| .getModel() | ||
| .expectShape(ShapeId.from("bsp#BuildTargetData")) | ||
| .expectTrait(classOf[DataTrait]) | ||
|
|
||
| // local shape | ||
| context | ||
| .getModel() | ||
| .expectShape(ShapeId.from("my.input#MyShape")) | ||
| .expectTrait(classOf[DataTrait]) | ||
|
|
||
| ModelTransformer | ||
| .create() | ||
| .mapTraits( | ||
| context.getModel(), | ||
| { | ||
| case (_, _: DataTrait) => | ||
| new DocumentationTrait("what's up doc") | ||
| case (_, trt) => trt | ||
| }: BiFunction[Shape, Trait, Trait] | ||
| ) | ||
| } | ||
|
|
||
| } |
19 changes: 19 additions & 0 deletions
19
modules/codegen-plugin/src/sbt-test/codegen-plugin/validator-trait-classes/build.sbt
This file contains hidden or 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,19 @@ | ||
| lazy val externalLibrary = project | ||
| .settings( | ||
| autoScalaLibrary := false, | ||
| crossPaths := false, | ||
| libraryDependencies ++= Seq( | ||
| "software.amazon.smithy" % "smithy-model" % "1.57.1" | ||
| ) | ||
| ) | ||
|
|
||
| lazy val root = project | ||
| .in(file(".")) | ||
| .enablePlugins(Smithy4sCodegenPlugin) | ||
| .settings( | ||
| scalaVersion := "3.3.6", | ||
| libraryDependencies ++= Seq( | ||
| "com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value | ||
| ), | ||
| Compile / smithy4sAllDependenciesAsJars += (externalLibrary / Compile / packageBin).value | ||
| ) |
39 changes: 39 additions & 0 deletions
39
...codegen-plugin/validator-trait-classes/externalLibrary/src/main/java/ApiVersionTrait.java
This file contains hidden or 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,39 @@ | ||
| /* | ||
| * Copyright 2021-2025 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. | ||
| */ | ||
|
|
||
| import software.amazon.smithy.model.SourceLocation; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.model.traits.StringTrait; | ||
|
|
||
| public class ApiVersionTrait extends StringTrait { | ||
|
|
||
| public static ShapeId ID = ShapeId.from("my.input#apiVersion"); | ||
|
|
||
| public ApiVersionTrait(String value, SourceLocation sourceLocation) { | ||
| super(ID, value, sourceLocation); | ||
| } | ||
|
|
||
| public ApiVersionTrait(String value) { | ||
| this(value, SourceLocation.NONE); | ||
| } | ||
|
|
||
| public static final class Provider extends StringTrait.Provider<ApiVersionTrait> { | ||
| public Provider() { | ||
| super(ID, ApiVersionTrait::new); | ||
| } | ||
| } | ||
|
|
||
| } |
38 changes: 38 additions & 0 deletions
38
...-plugin/validator-trait-classes/externalLibrary/src/main/java/ApiVersioningValidator.java
This file contains hidden or 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,38 @@ | ||
| /* | ||
| * Copyright 2021-2025 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. | ||
| */ | ||
|
|
||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.Shape; | ||
| import software.amazon.smithy.model.validation.AbstractValidator; | ||
| import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public final class ApiVersioningValidator extends AbstractValidator { | ||
|
|
||
| @Override | ||
| public List<ValidationEvent> validate(Model model) { | ||
| model.getShapesWithTrait(ApiVersionTrait.ID).stream().forEach(shape -> { | ||
| System.out.println("validating shape: " + shape); | ||
| shape.expectTrait(ApiVersionTrait.class); | ||
| }); | ||
|
|
||
| return List.of(); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...ary/src/main/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService
This file contains hidden or 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 @@ | ||
| ApiVersionTrait$Provider |
1 change: 1 addition & 0 deletions
1
...ry/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator
This file contains hidden or 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 @@ | ||
| ApiVersioningValidator |
9 changes: 9 additions & 0 deletions
9
...n/validator-trait-classes/externalLibrary/src/main/resources/META-INF/smithy/input.smithy
This file contains hidden or 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,9 @@ | ||
| $version: "2" | ||
|
|
||
| namespace my.input | ||
|
|
||
| @trait(selector: "service") | ||
| string apiVersion | ||
|
|
||
| @apiVersion("v1") | ||
| service FooService {} |
1 change: 1 addition & 0 deletions
1
...lugin/validator-trait-classes/externalLibrary/src/main/resources/META-INF/smithy/manifest
This file contains hidden or 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 @@ | ||
| input.smithy |
9 changes: 9 additions & 0 deletions
9
...es/codegen-plugin/src/sbt-test/codegen-plugin/validator-trait-classes/project/plugins.sbt
This file contains hidden or 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,9 @@ | ||
| sys.props.get("plugin.version") match { | ||
| case Some(x) => | ||
| addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % x) | ||
| case _ => | ||
| sys.error( | ||
| """|The system property 'plugin.version' is not defined. | ||
| |Specify this property using the scriptedLaunchOpts -D.""".stripMargin | ||
| ) | ||
| } |
3 changes: 3 additions & 0 deletions
3
modules/codegen-plugin/src/sbt-test/codegen-plugin/validator-trait-classes/test
This file contains hidden or 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 @@ | ||
| # check if the app compiles. | ||
| # if the validator throws, it's because of a bug like #336. | ||
| > compile |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -74,9 +74,14 @@ private[codegen] object ModelLoader { | |
| } | ||
| } | ||
|
|
||
| val validatorClassLoader = locally { | ||
| val jarUrls = deps.map(_.toURI().toURL()).toArray | ||
| new URLClassLoader(jarUrls, currentClassLoader) | ||
| } | ||
|
|
||
| // Loading the upstream model | ||
| val upstreamModel = Model | ||
| .assembler() | ||
| .assembler(validatorClassLoader) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should make the classes (validators, trait services) visible to the assembler - but notably not making the models visible.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // disabling cache to support snapshot-driven experimentation | ||
| .putProperty(ModelAssembler.DISABLE_JAR_CACHE, true) | ||
| .addClasspathModels(currentClassLoader, discoverModels) | ||
|
|
@@ -95,11 +100,6 @@ private[codegen] object ModelLoader { | |
| case _ => () | ||
| } | ||
|
|
||
| val validatorClassLoader = locally { | ||
| val jarUrls = deps.map(_.toURI().toURL()).toArray | ||
| new URLClassLoader(jarUrls, currentClassLoader) | ||
| } | ||
|
|
||
| val preTransformationModel = | ||
| Model | ||
| .assembler(validatorClassLoader) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: it is WAY too easy to misconfigure a transformation, hence this extra care
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW we had no other tests that involved user-provided transformations/validators, apparently. There's no
software.amazon.smithy.build.ProjectionTransformerorsoftware.amazon.smithy.model.validation.Validatorin the test resources. I think these tests suffice, but maybe we should have a more minimal one for both too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe indeed