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

Fix refined newtypes #1595

Merged
merged 7 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ When adding entries, please treat them as if they could end up in a release any

Thank you!

# 0.18.25

* Fixes an issue in which refinements wouldn't work on custom simple shapes (newtypes) (see [#1595](https://github.com/disneystreaming/smithy4s/pull/1595))

# 0.18.24

* Adds missing nanoseconds in Document encoding of EPOCH_SECOND timestamps
* Add support for `alloy#jsonUnknown`, allowing structures to capture unknown JSON fields in one of their members.
* Add support for `alloy#jsonUnknown`, allowing structures to capture unknown JSON fields in one of their members.
* Add `getMessage` implementation in `Smithy4sThrowable` which will be overridden in cases where the error structure contains a message field, but otherwise will be used to prevent a useless `null` result when `getMessage` is called.

# 0.18.23
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.struct

final case class HasConstrainedNewtype(s: CityId)

object HasConstrainedNewtype extends ShapeTag.Companion[HasConstrainedNewtype] {
val id: ShapeId = ShapeId("smithy4s.example", "HasConstrainedNewtype")

val hints: Hints = Hints.empty

// constructor using the original order from the spec
private def make(s: CityId): HasConstrainedNewtype = HasConstrainedNewtype(s)

implicit val schema: Schema[HasConstrainedNewtype] = struct(
CityId.schema.validated(smithy.api.Length(min = Some(1L), max = None)).required[HasConstrainedNewtype]("s", _.s),
)(make).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.struct

final case class HasConstrainedNewtypes(a: BucketName, b: CityId, c: Option[ObjectSize] = None, d: Option[IndexedSeq[String]] = None, e: Option[PNG] = None)

object HasConstrainedNewtypes extends ShapeTag.Companion[HasConstrainedNewtypes] {
val id: ShapeId = ShapeId("smithy4s.example", "HasConstrainedNewtypes")

val hints: Hints = Hints.empty

// constructor using the original order from the spec
private def make(a: BucketName, b: CityId, c: Option[ObjectSize], d: Option[IndexedSeq[String]], e: Option[PNG]): HasConstrainedNewtypes = HasConstrainedNewtypes(a, b, c, d, e)

implicit val schema: Schema[HasConstrainedNewtypes] = struct(
BucketName.schema.validated(smithy.api.Length(min = Some(1L), max = None)).required[HasConstrainedNewtypes]("a", _.a),
CityId.schema.validated(smithy.api.Length(min = Some(1L), max = None)).required[HasConstrainedNewtypes]("b", _.b),
ObjectSize.schema.validated(smithy.api.Range(min = Some(scala.math.BigDecimal(1.0)), max = None)).optional[HasConstrainedNewtypes]("c", _.c),
SomeIndexSeq.underlyingSchema.validated(smithy.api.Length(min = Some(1L), max = None)).optional[HasConstrainedNewtypes]("d", _.d),
PNG.schema.validated(smithy.api.Length(min = Some(1L), max = None)).optional[HasConstrainedNewtypes]("e", _.e),
)(make).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private[codegen] object ModelLoader {
val modelsInJars = deps.flatMap { file =>
Using.resource(
// Note: On JDK13+, the second parameter is redundant.
FileSystems.newFileSystem(file.toPath(), null)
FileSystems.newFileSystem(file.toPath(), null: ClassLoader)
) { jarFS =>
val p = jarFS.getPath("META-INF", "smithy", "manifest")

Expand Down
9 changes: 7 additions & 2 deletions modules/core/src/smithy4s/RefinementProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@ private[smithy4s] trait LowPriorityImplicits {
: RefinementProvider[Pattern, E, E] =
new RefinementProvider.PatternConstraint[E](e => e.value)

implicit def isomorphismConstraint[C, A, A0](implicit
@deprecated("Use isomorphismConstraint2 instead", "0.18.25")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: for bincompat, we have to keep the old method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to make it private[smithy4s] but MiMa complained about the static forwarder disappearing. Figured I'd restore the method to the public scope, it doesn't hurt if it's not implicit anymore.

private[smithy4s] def isomorphismConstraint[C, A, A0](implicit
constraintOnA: RefinementProvider.Simple[C, A],
iso: Bijection[A, A0]
): RefinementProvider[C, A0, A0] = constraintOnA.imapFull[A0, A0](iso, iso)
): RefinementProvider[C, A0, A0] = isomorphismConstraint2

implicit def isomorphismConstraint2[C, A, A0](implicit
iso: Bijection[A, A0],
constraintOnA: RefinementProvider.Simple[C, A]
): RefinementProvider[C, A0, A0] = constraintOnA.imapFull[A0, A0](iso, iso)
}
20 changes: 20 additions & 0 deletions sampleSpecs/memberConstraints.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ map ConstrainedMap {
value: String
}

// Regression test for https://github.com/disneystreaming/smithy4s/issues/1594
structure HasConstrainedNewtypes {
// string newtype
@length(min: 1)
@required
a: BucketName
// string newtype, double-constrained
@length(min: 1)
@required
b: CityId
// int newtype
@range(min: 1)
c: ObjectSize
// list newtype, oh wait these are just lists. Still.
@length(min: 1)
d: SomeIndexSeq
// blob newtype
@length(min: 1)
e: PNG
}
Loading