Contract is not truly type safe
See original GitHub issueHello, I wanted to leverage the ‘type safe’ contract http4k claims to provide to ensure (at compile time) that a handler for given route/method responds with correct response type. However, from the available documentation I was not able to figure out how to do that or whether that’s even possible.
Consider following example
data class City(
val id: Long,
val name: String,
)
fun getCity(): ContractRoute {
val openApiSpec = "city/v1/get" meta {
// type of the response is defined here to be City
returning(Status.OK, Body.auto<City>().toLens() to City(id = 123, name = "Prague"))
} bindContract Method.GET
return openApiSpec to {
// this is expected to fail at compile type because it does not conform to the spec above
Response(Status.OK, "booya!")
}
}
Can the expectation be fulfilled? If so, how?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Unenforceable Contracts: What to Watch Out For | Nolo
Contracts can be found unenforceable on grounds of public policy not only to protect one of the parties involved, but also because what...
Read more >SAFE Agreement: How They Work, 5 Important Terms (2022)
SAFE agreements are legal contracts that startups use to raise seed financing ... The type of instrument you choose depends upon the startup...
Read more >Type safety - Wikipedia
Type safety in object oriented languages A class essentially defines the structure of the objects derived from it and an API as a...
Read more >Runtime typesafety in typescript - angular - Stack Overflow
Back to the question about safety, yes TypeScript ensure safety during writing the code. You define the contract, write transformations of ...
Read more >Advanced Topic - Agile Contracts - Scaled Agile Framework
The 'SAFe managed-investment contract' represents one such approach, as described below.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep - we’re aware of this and you are of course correct. There is a fine balance between making the API comprehensible (and avoiding magic), which is obviously a little out of whack here.
… that said - there is a bit of a side project that we’ve been playing with to come up with a completely typesafe routing module which can be bound to create both server and client bindings. It’s early days, but we’re quite hopeful it might be something that has legs… 😃
There is no news on this feature request.