-
Notifications
You must be signed in to change notification settings - Fork 68
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
Default handling #1315
Merged
kubukoz
merged 10 commits into
disneystreaming:series/0.18
from
msosnicki:default-values-handling
Jan 19, 2024
Merged
Default handling #1315
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b360577
WIP
msosnicki a8b545b
Skip none values
msosnicki 0df9929
Fix compilation for scala 2.12
msosnicki 1accdd7
Update docs + add new param to document encoder visitor
msosnicki 02ad146
PR comments
msosnicki 87881d6
Update test
msosnicki 60ccc6c
PR comments - move tests and improve UX
msosnicki 107eb97
Merge branch 'series/0.18' into default-values-handling
msosnicki 6fe578c
Update the changelog
msosnicki eb5f1b9
Regenerate files
msosnicki 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
156 changes: 156 additions & 0 deletions
156
modules/bootstrapped/resources/smithy4s.example.ServiceWithNullsAndDefaults.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,156 @@ | ||
{ | ||
"openapi": "3.0.2", | ||
"info": { | ||
"title": "ServiceWithNullsAndDefaults", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/operation/{requiredLabel}": { | ||
"post": { | ||
"operationId": "Operation", | ||
"requestBody": { | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/OperationRequestContent" | ||
} | ||
} | ||
}, | ||
"required": true | ||
}, | ||
"parameters": [ | ||
{ | ||
"name": "requiredLabel", | ||
"in": "path", | ||
"schema": { | ||
"type": "string", | ||
"default": "required-label-with-default" | ||
}, | ||
"required": true | ||
}, | ||
{ | ||
"name": "optional-query", | ||
"in": "query", | ||
"schema": { | ||
"type": "string" | ||
} | ||
}, | ||
{ | ||
"name": "optional-query-with-default", | ||
"in": "query", | ||
"schema": { | ||
"type": "string", | ||
"default": "optional-query-with-default" | ||
} | ||
}, | ||
{ | ||
"name": "required-query-with-default", | ||
"in": "query", | ||
"schema": { | ||
"type": "string", | ||
"default": "required-query-with-default" | ||
} | ||
}, | ||
{ | ||
"name": "optional-header", | ||
"in": "header", | ||
"schema": { | ||
"type": "string" | ||
} | ||
}, | ||
{ | ||
"name": "optional-header-with-default", | ||
"in": "header", | ||
"schema": { | ||
"type": "string", | ||
"default": "optional-header-with-default" | ||
} | ||
}, | ||
{ | ||
"name": "required-header-with-default", | ||
"in": "header", | ||
"schema": { | ||
"type": "string", | ||
"default": "required-header-with-default" | ||
}, | ||
"required": true | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "Operation 200 response", | ||
"headers": { | ||
"optional-header": { | ||
"schema": { | ||
"type": "string" | ||
} | ||
}, | ||
"optional-header-with-default": { | ||
"schema": { | ||
"type": "string", | ||
"default": "optional-header-with-default" | ||
} | ||
}, | ||
"required-header-with-default": { | ||
"schema": { | ||
"type": "string", | ||
"default": "required-header-with-default" | ||
}, | ||
"required": true | ||
} | ||
}, | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/OperationResponseContent" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"OperationRequestContent": { | ||
"type": "object", | ||
"properties": { | ||
"optional": { | ||
"type": "string" | ||
}, | ||
"optionalWithDefault": { | ||
"type": "string", | ||
"default": "optional-default" | ||
}, | ||
"requiredWithDefault": { | ||
"type": "string", | ||
"default": "required-default" | ||
} | ||
}, | ||
"required": [ | ||
"requiredWithDefault" | ||
] | ||
}, | ||
"OperationResponseContent": { | ||
"type": "object", | ||
"properties": { | ||
"optional": { | ||
"type": "string" | ||
}, | ||
"optionalWithDefault": { | ||
"type": "string", | ||
"default": "optional-default" | ||
}, | ||
"requiredWithDefault": { | ||
"type": "string", | ||
"default": "required-default" | ||
} | ||
}, | ||
"required": [ | ||
"requiredWithDefault" | ||
] | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
modules/bootstrapped/src/generated/smithy4s/example/OperationInput.scala
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,33 @@ | ||
package smithy4s.example | ||
|
||
import smithy4s.Hints | ||
import smithy4s.Schema | ||
import smithy4s.ShapeId | ||
import smithy4s.ShapeTag | ||
import smithy4s.schema.Schema.string | ||
import smithy4s.schema.Schema.struct | ||
|
||
final case class OperationInput(optionalWithDefault: String = "optional-default", requiredLabel: String = "required-label-with-default", requiredWithDefault: String = "required-default", optionalHeaderWithDefault: String = "optional-header-with-default", requiredHeaderWithDefault: String = "required-header-with-default", optionalQueryWithDefault: String = "optional-query-with-default", requiredQueryWithDefault: String = "required-query-with-default", optional: Option[String] = None, optionalHeader: Option[String] = None, optionalQuery: Option[String] = None) | ||
|
||
object OperationInput extends ShapeTag.Companion[OperationInput] { | ||
val id: ShapeId = ShapeId("smithy4s.example", "OperationInput") | ||
|
||
val hints: Hints = Hints( | ||
smithy.api.Input(), | ||
) | ||
|
||
implicit val schema: Schema[OperationInput] = struct( | ||
string.field[OperationInput]("optionalWithDefault", _.optionalWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("optional-default"))), | ||
string.required[OperationInput]("requiredLabel", _.requiredLabel).addHints(smithy.api.Default(smithy4s.Document.fromString("required-label-with-default")), smithy.api.HttpLabel()), | ||
string.required[OperationInput]("requiredWithDefault", _.requiredWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("required-default"))), | ||
string.field[OperationInput]("optionalHeaderWithDefault", _.optionalHeaderWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("optional-header-with-default")), smithy.api.HttpHeader("optional-header-with-default")), | ||
string.required[OperationInput]("requiredHeaderWithDefault", _.requiredHeaderWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("required-header-with-default")), smithy.api.HttpHeader("required-header-with-default")), | ||
string.field[OperationInput]("optionalQueryWithDefault", _.optionalQueryWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("optional-query-with-default")), smithy.api.HttpQuery("optional-query-with-default")), | ||
string.field[OperationInput]("requiredQueryWithDefault", _.requiredQueryWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("required-query-with-default")), smithy.api.HttpQuery("required-query-with-default")), | ||
string.optional[OperationInput]("optional", _.optional), | ||
string.optional[OperationInput]("optionalHeader", _.optionalHeader).addHints(smithy.api.HttpHeader("optional-header")), | ||
string.optional[OperationInput]("optionalQuery", _.optionalQuery).addHints(smithy.api.HttpQuery("optional-query")), | ||
){ | ||
OperationInput.apply | ||
}.withId(id).addHints(hints) | ||
} |
29 changes: 29 additions & 0 deletions
29
modules/bootstrapped/src/generated/smithy4s/example/OperationOutput.scala
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,29 @@ | ||
package smithy4s.example | ||
|
||
import smithy4s.Hints | ||
import smithy4s.Schema | ||
import smithy4s.ShapeId | ||
import smithy4s.ShapeTag | ||
import smithy4s.schema.Schema.string | ||
import smithy4s.schema.Schema.struct | ||
|
||
final case class OperationOutput(optionalWithDefault: String = "optional-default", requiredWithDefault: String = "required-default", optionalHeaderWithDefault: String = "optional-header-with-default", requiredHeaderWithDefault: String = "required-header-with-default", optional: Option[String] = None, optionalHeader: Option[String] = None) | ||
|
||
object OperationOutput extends ShapeTag.Companion[OperationOutput] { | ||
val id: ShapeId = ShapeId("smithy4s.example", "OperationOutput") | ||
|
||
val hints: Hints = Hints( | ||
smithy.api.Output(), | ||
) | ||
|
||
implicit val schema: Schema[OperationOutput] = struct( | ||
string.field[OperationOutput]("optionalWithDefault", _.optionalWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("optional-default"))), | ||
string.required[OperationOutput]("requiredWithDefault", _.requiredWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("required-default"))), | ||
string.field[OperationOutput]("optionalHeaderWithDefault", _.optionalHeaderWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("optional-header-with-default")), smithy.api.HttpHeader("optional-header-with-default")), | ||
string.required[OperationOutput]("requiredHeaderWithDefault", _.requiredHeaderWithDefault).addHints(smithy.api.Default(smithy4s.Document.fromString("required-header-with-default")), smithy.api.HttpHeader("required-header-with-default")), | ||
string.optional[OperationOutput]("optional", _.optional), | ||
string.optional[OperationOutput]("optionalHeader", _.optionalHeader).addHints(smithy.api.HttpHeader("optional-header")), | ||
){ | ||
OperationOutput.apply | ||
}.withId(id).addHints(hints) | ||
} |
85 changes: 85 additions & 0 deletions
85
modules/bootstrapped/src/generated/smithy4s/example/ServiceWithNullsAndDefaults.scala
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,85 @@ | ||
package smithy4s.example | ||
|
||
import smithy4s.Endpoint | ||
import smithy4s.Hints | ||
import smithy4s.Schema | ||
import smithy4s.Service | ||
import smithy4s.ShapeId | ||
import smithy4s.Transformation | ||
import smithy4s.kinds.PolyFunction5 | ||
import smithy4s.kinds.toPolyFunction5.const5 | ||
import smithy4s.schema.OperationSchema | ||
|
||
trait ServiceWithNullsAndDefaultsGen[F[_, _, _, _, _]] { | ||
self => | ||
|
||
def operation(input: OperationInput): F[OperationInput, Nothing, OperationOutput, Nothing, Nothing] | ||
|
||
def transform: Transformation.PartiallyApplied[ServiceWithNullsAndDefaultsGen[F]] = Transformation.of[ServiceWithNullsAndDefaultsGen[F]](this) | ||
} | ||
|
||
object ServiceWithNullsAndDefaultsGen extends Service.Mixin[ServiceWithNullsAndDefaultsGen, ServiceWithNullsAndDefaultsOperation] { | ||
|
||
val id: ShapeId = ShapeId("smithy4s.example", "ServiceWithNullsAndDefaults") | ||
val version: String = "1.0.0" | ||
|
||
val hints: Hints = Hints( | ||
alloy.SimpleRestJson(), | ||
) | ||
|
||
def apply[F[_]](implicit F: Impl[F]): F.type = F | ||
|
||
object ErrorAware { | ||
def apply[F[_, _]](implicit F: ErrorAware[F]): F.type = F | ||
type Default[F[+_, +_]] = Constant[smithy4s.kinds.stubs.Kind2[F]#toKind5] | ||
} | ||
|
||
val endpoints: Vector[smithy4s.Endpoint[ServiceWithNullsAndDefaultsOperation, _, _, _, _, _]] = Vector( | ||
ServiceWithNullsAndDefaultsOperation.Operation, | ||
) | ||
|
||
def input[I, E, O, SI, SO](op: ServiceWithNullsAndDefaultsOperation[I, E, O, SI, SO]): I = op.input | ||
def ordinal[I, E, O, SI, SO](op: ServiceWithNullsAndDefaultsOperation[I, E, O, SI, SO]): Int = op.ordinal | ||
override def endpoint[I, E, O, SI, SO](op: ServiceWithNullsAndDefaultsOperation[I, E, O, SI, SO]) = op.endpoint | ||
class Constant[P[-_, +_, +_, +_, +_]](value: P[Any, Nothing, Nothing, Nothing, Nothing]) extends ServiceWithNullsAndDefaultsOperation.Transformed[ServiceWithNullsAndDefaultsOperation, P](reified, const5(value)) | ||
type Default[F[+_]] = Constant[smithy4s.kinds.stubs.Kind1[F]#toKind5] | ||
def reified: ServiceWithNullsAndDefaultsGen[ServiceWithNullsAndDefaultsOperation] = ServiceWithNullsAndDefaultsOperation.reified | ||
def mapK5[P[_, _, _, _, _], P1[_, _, _, _, _]](alg: ServiceWithNullsAndDefaultsGen[P], f: PolyFunction5[P, P1]): ServiceWithNullsAndDefaultsGen[P1] = new ServiceWithNullsAndDefaultsOperation.Transformed(alg, f) | ||
def fromPolyFunction[P[_, _, _, _, _]](f: PolyFunction5[ServiceWithNullsAndDefaultsOperation, P]): ServiceWithNullsAndDefaultsGen[P] = new ServiceWithNullsAndDefaultsOperation.Transformed(reified, f) | ||
def toPolyFunction[P[_, _, _, _, _]](impl: ServiceWithNullsAndDefaultsGen[P]): PolyFunction5[ServiceWithNullsAndDefaultsOperation, P] = ServiceWithNullsAndDefaultsOperation.toPolyFunction(impl) | ||
|
||
} | ||
|
||
sealed trait ServiceWithNullsAndDefaultsOperation[Input, Err, Output, StreamedInput, StreamedOutput] { | ||
def run[F[_, _, _, _, _]](impl: ServiceWithNullsAndDefaultsGen[F]): F[Input, Err, Output, StreamedInput, StreamedOutput] | ||
def ordinal: Int | ||
def input: Input | ||
def endpoint: Endpoint[ServiceWithNullsAndDefaultsOperation, Input, Err, Output, StreamedInput, StreamedOutput] | ||
} | ||
|
||
object ServiceWithNullsAndDefaultsOperation { | ||
|
||
object reified extends ServiceWithNullsAndDefaultsGen[ServiceWithNullsAndDefaultsOperation] { | ||
def operation(input: OperationInput): Operation = Operation(input) | ||
} | ||
class Transformed[P[_, _, _, _, _], P1[_ ,_ ,_ ,_ ,_]](alg: ServiceWithNullsAndDefaultsGen[P], f: PolyFunction5[P, P1]) extends ServiceWithNullsAndDefaultsGen[P1] { | ||
def operation(input: OperationInput): P1[OperationInput, Nothing, OperationOutput, Nothing, Nothing] = f[OperationInput, Nothing, OperationOutput, Nothing, Nothing](alg.operation(input)) | ||
} | ||
|
||
def toPolyFunction[P[_, _, _, _, _]](impl: ServiceWithNullsAndDefaultsGen[P]): PolyFunction5[ServiceWithNullsAndDefaultsOperation, P] = new PolyFunction5[ServiceWithNullsAndDefaultsOperation, P] { | ||
def apply[I, E, O, SI, SO](op: ServiceWithNullsAndDefaultsOperation[I, E, O, SI, SO]): P[I, E, O, SI, SO] = op.run(impl) | ||
} | ||
final case class Operation(input: OperationInput) extends ServiceWithNullsAndDefaultsOperation[OperationInput, Nothing, OperationOutput, Nothing, Nothing] { | ||
def run[F[_, _, _, _, _]](impl: ServiceWithNullsAndDefaultsGen[F]): F[OperationInput, Nothing, OperationOutput, Nothing, Nothing] = impl.operation(input) | ||
def ordinal: Int = 0 | ||
def endpoint: smithy4s.Endpoint[ServiceWithNullsAndDefaultsOperation,OperationInput, Nothing, OperationOutput, Nothing, Nothing] = Operation | ||
} | ||
object Operation extends smithy4s.Endpoint[ServiceWithNullsAndDefaultsOperation,OperationInput, Nothing, OperationOutput, Nothing, Nothing] { | ||
val schema: OperationSchema[OperationInput, Nothing, OperationOutput, Nothing, Nothing] = Schema.operation(ShapeId("smithy4s.example", "Operation")) | ||
.withInput(OperationInput.schema) | ||
.withOutput(OperationOutput.schema) | ||
.withHints(smithy.api.Http(method = smithy.api.NonEmptyString("POST"), uri = smithy.api.NonEmptyString("/operation/{requiredLabel}"), code = 200)) | ||
def wrap(input: OperationInput): Operation = Operation(input) | ||
} | ||
} | ||
|
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
not a fan of this.
awsHeaderEncoding
shouldn't be exposed to the users here.Also, the UX I'd like would be :
Encoder.withExplicitDefaultsEncoding(true)
. TheCachedEncoderCompilerImpl
class should carry the method, which implies that we need to create an public interface for this that it will implement (asCachedEncoderCompilerImpl
is private).Also this
make
method should be removed.