haskell-servant / haskell-servant/servant

Read-Only and Write-Only properties in Servant

Open
#1,025 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
2k
Forks
427
Avg merge
2d 23h
Merged PRs (30d)
5

Description

Swagger has "readOnly" and "writeOnly" keywords:
https://swagger.io/docs/specification/data-models/data-types/#readonly-writeonly
They are very practical when some properties appear in the data returned by GET but not in a POST (or vice-versa).
For example:
```
type: object
properties:
id:
# Returned by GET, not used in POST/PUT/PATCH
type: integer
_readOnly: true_
username:
type: string
password:
# Used in POST/PUT/PATCH, not returned by GET
type: string
_writeOnly: true_
```

How to do that in Servant?
I could use Maybes:
```
data User = User {
id :: Maybe Text,
username: Text,
password: Maybe Text
}

type UserAPI = "users" :> Get '[JSON] [User]
:<|> "users" :> ReqBody '[JSON] User :> Post '[JSON] ()
```

However, the implementer will be responsible to fill or not the id and password.
Is there a way to make this in a more type safe way?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.