luckyframework / luckyframework/avram

Param nesting should be optional for Avram::Operation

Open
#355 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Crystal
Stars
183
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Currently, if you want to create an operation and pull fields off of the params, you have to make sure the fields are nested. For example:

```crystal
class Search < AvramOperation
attribute query : String

def submit
puts breed.value
end
end
```

To use this operation, the `query` attribute has to be nested within a `search` key. In JSON, this would look like:

```json
{
"search": {
"query": "my query"
}
}
```

I am currently experiencing two issues with this right now:

1. An error is raised if you initialize an operation with params and the param_key is missing.
2. It is not possible to use params if you do not want to use nested attributes

These two issues stem from wanting to use an `Avram::Operation` for a search endpoint. Ideally I would have the endpoint look like `https://www.example.com/search?q=my-search` and be able to handle when no query is passed in as well as the flat param. Instead, I have to check for the nested param before creating the operation and not pass in params if the nested param is missing.

The relevant code is here: https://github.com/luckyframework/avram/blob/6bae55c6e580e676eaa47aa3cb0baa4fbc5f3656/src/avram/define_attribute.cr#L86-L88

Instead of forcing nesting, I believe it should be optional. Unless there is a solution that I'm missing, this would seem to be a problem anytime you'd like to pass in query parameters into a GET request or similar situations.

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 at src/avram/define_attribute.cr around lines 86-88, where the issue identifies the required parameter nesting. Reproduce the nested, flat, and missing-param cases described in the issue. Done means an Avram::Operation can accept flat query parameters and can be initialized without the nested param key.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal
Domain
backend
Issue type
Feature
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.