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 passing in the MaxArity for the JsonSchemaVisitor #569

Merged
merged 19 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ The `SimpleRestJson` protocol supports 3 different union encodings :

See the section about [unions](../../04-codegen/02-unions.md) for a detailed description.

## Json Array Arity
* By default there is a limit on the arity of an array, which is 1024. This is to prevent the server from being overloaded with a large array as this is a vector for attacks.
* This limit can be changed by setting the maxArity `smithy4s.http4s.SimpleRestJsonBuilder.withMaxArity(.)` to the desired value.
* an example can be seen in the [client example](03-client.md)
## Supported traits

Here is the list of traits supported by `SimpleRestJson`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ title: SimpleRestJson client
The `smithy4s-http4s` module provides functions that transform low-level http4s clients into high-level stubs, provided the corresponding service definitions (in smithy) are annotated with the `alloy#simpleRestJson` protocol.
- Uri is optional as it will default to http://localhost:8080
In `build.sbt`
- the max arity of json array decoder is 1024 by default, but can be changed by calling withMaxArity on the RestJsonBuilder object

```scala
libraryDependencies ++= Seq(
Expand Down Expand Up @@ -33,9 +34,10 @@ object Clients {
.uri(Uri.unsafeFromString("http://localhost"))
.resource

// alternatively ...
// alternatively ... with setting the max arity to 2048
def helloWorldClient2(http4sClient: Client[IO]) : Resource[IO, HelloWorldService[IO]] =
SimpleRestJsonBuilder(HelloWorldService)
SimpleRestJsonBuilder
.withMaxArity(2048)(HelloWorldService)
.client(http4sClient)
.uri(Uri.unsafeFromString("http://localhost"))
.resource
Expand Down
12 changes: 9 additions & 3 deletions modules/http4s/src/smithy4s/http4s/SimpleRestJsonBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ package http4s

import smithy4s.internals.InputOutput

object SimpleRestJsonBuilder
object SimpleRestJsonBuilder extends SimpleRestJsonBuilder(1024)

class SimpleRestJsonBuilder(maxArity: Int)
extends SimpleProtocolBuilder[alloy.SimpleRestJson](
smithy4s.http.json.codecs(
alloy.SimpleRestJson.protocol.hintMask ++ HintMask(
InputOutput,
IntEnum
)
),
maxArity
)
)
) {
def withMaxArity(maxArity: Int): SimpleRestJsonBuilder =
new SimpleRestJsonBuilder(maxArity)
}
16 changes: 11 additions & 5 deletions modules/json/src/smithy4s/http/json/codecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import smithy4s.schema.SchemaVisitor
import smithy4s.schema.CompilationCache

final case class codecs(
hintMask: HintMask = codecs.defaultHintMask
) extends JsonCodecAPI(codecs.schemaVisitorJCodec(_), Some(hintMask))
hintMask: HintMask = codecs.defaultHintMask,
maxArity: Int = codecs.defaultMaxArity
) extends JsonCodecAPI(codecs.schemaVisitorJCodec(_, maxArity), Some(hintMask))

object codecs {

Expand All @@ -43,13 +44,18 @@ object codecs {
// TODO: add tests for `codecs` understanding int enums. Maybe pizza spec
IntEnum
)
val defaultMaxArity: Int = 1024

private[smithy4s] def schemaVisitorJCodec(
cache: CompilationCache[JCodec]
cache: CompilationCache[JCodec],
maxArity: Int = defaultMaxArity
): SchemaVisitor[JCodec] =
new SchemaVisitorJCodec(maxArity = 1024, cache)
new SchemaVisitorJCodec(maxArity, cache)

private[smithy4s] val schemaVisitorJCodec: SchemaVisitor[JCodec] =
new SchemaVisitorJCodec(maxArity = 1024, CompilationCache.nop[JCodec])
new SchemaVisitorJCodec(
maxArity = defaultMaxArity,
CompilationCache.nop[JCodec]
)
Comment on lines +56 to +59
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this version currently used for? From a quick look it seems like only in tests. Maybe we can remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Im not sure what you mean by version?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's add some documentation showing how to increase the maxArity and what that means

good point

Copy link
Contributor

Choose a reason for hiding this comment

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

Im not sure what you mean by version?

Yeah sorry that is super confusing! I meant the "Val" vs the "def" above. The val I'm not sure is used anywhere. Thought we could remove it if that is in fact the case.


}