Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize requests at API level before validation #4136

Merged
merged 2 commits into from
Jun 27, 2024
Merged

Conversation

wdbaruni
Copy link
Member

Today we only normalize and validate requests at the client side, but only validate on the server side without normalizing first. This causes inconsistencies and issues around optional fields, such as publishers.

Example before the fix:

# hello.json
{
  "Job": {
    "Type": "batch",
    "Count": 1,
    "Tasks": [
      {
        "Name": "main",
        "Engine": {
          "Type": "docker",
          "Params": {
            "Image": "ubuntu",
            "Parameters": ["echo", "hello"]
          }
        }
      }
    ]
  }
}
 curl -X PUT localhost:1234/api/v1/orchestrator/jobs -H "Content-Type: application/json" -d @hello.json
{
  "error": "code=400, message=task main validation failed: publisher validation failed: nil spec config",
  "message": "task main validation failed: publisher validation failed: nil spec config"
}

Example after the fix

→  curl -X PUT localhost:1234/api/v1/orchestrator/jobs -H "Content-Type: application/json" -d @hello.json

{
  "JobID": "j-26b215f9-582c-4374-80cb-fd079e73fc37",
  "EvaluationID": "36fd6f65-64fd-421b-a9ba-aaf86d876704",
  "Warnings": null
}
→ bacalhau job describe j-26b215f9-582c-4374-80cb-fd079e73fc37
ID            = j-26b215f9-582c-4374-80cb-fd079e73fc37
Name          = j-26b215f9-582c-4374-80cb-fd079e73fc37
Namespace     = default
Type          = batch
State         = Completed
Count         = 1
Created Time  = 2024-06-25 09:14:40
Modified Time = 2024-06-25 09:14:43
Version       = 0

Summary
Completed = 1

Job History
 TIME                 REV.  STATE      TOPIC       EVENT
 2024-06-25 11:14:40  1     Pending    Submission  Job submitted
 2024-06-25 11:14:40  2     Running
 2024-06-25 11:14:43  3     Completed

Executions
 ID          NODE ID     STATE      DESIRED  REV.  CREATED  MODIFIED  COMMENT
 e-ad7cc59d  n-6e8998ad  Completed  Stopped  6     14s ago  12s ago   Accepted job

Execution e-ad7cc59d History
 TIME                 REV.  STATE              TOPIC            EVENT
 2024-06-25 11:14:40  1     New
 2024-06-25 11:14:40  2     AskForBid
 2024-06-25 11:14:40  3     AskForBidAccepted  Requesting Node  Accepted job
 2024-06-25 11:14:40  4     AskForBidAccepted
 2024-06-25 11:14:40  5     BidAccepted
 2024-06-25 11:14:43  6     Completed

Standard Output
hello
→ bacalhau job describe j-26b215f9-582c-4374-80cb-fd079e73fc37 --output json --pretty | jq ".Job"
{
  "ID": "j-26b215f9-582c-4374-80cb-fd079e73fc37",
  "Name": "j-26b215f9-582c-4374-80cb-fd079e73fc37",
  "Namespace": "default",
  "Type": "batch",
  "Priority": 0,
  "Count": 1,
  "Constraints": [],
  "Meta": {
    "bacalhau.org/requester.id": "n-6e8998ad-e0b9-43e6-9ccb-47f05a780f4a"
  },
  "Labels": {},
  "Tasks": [
    {
      "Name": "main",
      "Engine": {
        "Type": "docker",
        "Params": {
          "Image": "ubuntu",
          "Parameters": [
            "echo",
            "hello"
          ]
        }
      },
      "Publisher": {
        "Type": ""
      },
      "Resources": {},
      "Network": {
        "Type": "None"
      },
      "Timeouts": {
        "TotalTimeout": 1800
      }
    }
  ],
  "State": {
    "StateType": "Completed"
  },
  "Version": 0,
  "Revision": 3,
  "CreateTime": 1719306880611865000,
  "ModifyTime": 1719306883073224000
}

Closes #3950

@wdbaruni wdbaruni self-assigned this Jun 25, 2024
Copy link

coderabbitai bot commented Jun 25, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@wdbaruni wdbaruni requested a review from frrist June 25, 2024 09:16
@wdbaruni wdbaruni added the comp/api Issues specifically related to the API layer label Jun 25, 2024
Copy link
Member

@frrist frrist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this will guarantee types are normalized after the server binds args? Sounds great.

}

// CustomBinder is a custom binder that extends the default binder with normalization.
type CustomBinder struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about NormalizeBinder

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed in 8151aad

@wdbaruni wdbaruni merged commit 960ceb1 into main Jun 27, 2024
9 of 12 checks passed
@wdbaruni wdbaruni deleted the normalize-api-level branch June 27, 2024 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp/api Issues specifically related to the API layer
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Validation in API layer
2 participants