metosin / metosin/compojure-api

Issue with enum query parameter with a default value

Open
#287 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug upstream-problem
Dominant language
Clojure
Stars
1.1k
Forks
146
PR merge metrics
No merged PRs in 30d

Description

If a query parameter is of type enum and has a default value, the swagger parameter for default should be appropriately set and required should be set to true.

Here is an example below.

what's happening
(GET "/all/" request
     :description "Return a list of articles."
     :return [(dissoc t/Article :is_html :text)]
     :responses default-responses
     :query-params [{sort :- (describe (s/enum :published_at :last_processed_at) "The sort type. Defaults to published_at.") "published_at"}]
     (return (domain/get-all-articles db (org request) :days 1)))

The sort parameter above can be either published_at or last_processed_at where it defaults to the former.

The JSON output for the route above is:

"parameters": [
  {
    "in": "query",
    "name": "sort",
    "description": "The sort type. Defaults to published_at.",
    "required": false,
    "type": "string",
    "enum": [
      "last_processed_at",
      "published_at"
    ]
  }
]

And the UI renders like this:

screen shot 2017-03-18 at 9 59 34 pm

expectation

I would expect the select box in the UI to start off with the published_at pre-selected and not allow a nil option.
I would expect the json output to have required set to true and a default value set to published_at.

Thanks for your time!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Swagger parameter generation for the enum query parameter shown in the issue. Check how the default value and required flag are derived, then verify that the generated JSON includes default "published_at" and required true, with no nil option in the UI.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.