metosin / metosin/compojure-api
inconsistency in resource vs. macro based route definition behaviour
Nobody has claimed this yet.
- Dominant language
- Clojure
- Stars
- 1.1k
- Forks
- 146
- PR merge metrics
- No merged PRs in 30d
Description
When making:
```
curl -X POST \
'http://localhost:7777/foo?rule_name=foo.logic&force_overwrite=true&version=1.1' \
-H 'accept: application/json' \
-H 'cache-control: no-cache' \
-H 'content-type: text/plain' \
-d 'bla'
```
the following definition behaves correctly:
```
(POST "/foo" []
:middleware [[muuntaja.middleware/wrap-format
(muuntaja/create
(assoc-in muuntaja/default-options
[:formats "text/plain"] text-plain-format))]]
:query-params [version :- String, rule_name :- String, force_overwrite :- String]
:body [b s/Str]
:return s/Any
(ok
{:ok b})
```
on other hand this one is failing
```
(context "/foo" []
(resource {
:post {
:middleware [[muuntaja.middleware/wrap-format
(muuntaja/create
(assoc-in muuntaja/default-options
[:formats "text/plain"] text-plain-format))]]
:parameters {:body-params String
:query-params {:version s/Str
:rule_name s/Str
:force_overwrite s/Str}}
:consumes ["text/plain"]
:responses {http-status/ok {:schema s/Any}}
:handler (fn [{body :body-params {:keys [version rule_name]} :path-params}]
(ok
{:foo body}
))}}))
```
the error is:
```
{
"schema": "java.lang.String",
"errors": "(not (instance? java.lang.String nil))",
"type": "compojure.api.exception/request-validation",
"coercion": "schema",
"value": null,
"in": [
"request",
"body-params"
]
}
```
I use this auxiliary fun:
```
(def text-plain-format
{:decoder [(fn make-json-decoder [options]
(fn [x ^String charset]
(if (string? x)
x
(slurp (InputStreamReader. ^InputStream x charset)))))]
:encoder [(fn make-json-encoder [options]
(fn [data ^String charset]
(ByteArrayInputStream. (.getBytes data charset))))]})
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the POST /foo curl request, then compare the macro-based route with the context/resource definition. Start at the resource route's body-parameter validation and the custom text/plain middleware; done means both route forms handle the same request consistently without the nil String validation error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- clojure
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100