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

[Rocket] Typehint String on application/octet-stream #65

Closed
Nukesor opened this issue Oct 19, 2021 · 2 comments
Closed

[Rocket] Typehint String on application/octet-stream #65

Nukesor opened this issue Oct 19, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@Nukesor
Copy link
Contributor

Nukesor commented Oct 19, 2021

Hi :)

First of all, thanks for creating and maintaining okapi. It's super helpful!

I stumbled upon a little issue when designing routes for file upload.
In our design, files could be uploaded in raw form as an application/octet-stream to our route.
As far as I understand, the expected way of doing this in rocket is to use the Data helper.

However in your code-base, there's currently String set as the type for the stream. One would expecte [u8] for an octet stream.

#[openapi(tag = "files")]
#[post("/<entity_id>", data = "<data>")]
pub async fn create_file(
    entity_id: Uuid,
    data: Data<'_>,
    ...

The generated spec:

        "requestBody": {
          "content": {
            "application/octet-stream": {
              "schema": {
                "type": "string"
              }
            }
          },
          "required": true
        },

The expected spec:

        "requestBody": {
          "content": {
            "application/octet-stream": {
              "schema": {
                "type": "array",
                "items": {
                  "type": "integer",
                  "format": "uint8",
                  "minimum": 0.0
                }
              }
            }
          },
          "required": true
        },

This popped up as an issue, when we tried to upload files via our frontend, but the typechecks failed as the schema expected a String.
Using &[u8] instead of Data works fine and crates the correct schema, however that's not how one works with files in Rocket and brings its own ploblems with Rocket's default data limits.

I'm not 100% sure if this is indeed a bug, of if we're just abusing the spec.

@ralpha ralpha self-assigned this Oct 19, 2021
@Nukesor Nukesor changed the title [Rocket] Typehint String on Data application/octet-stream [Rocket] Typehint String on application/octet-stream Oct 19, 2021
@ralpha ralpha added the bug Something isn't working label Oct 19, 2021
@ralpha ralpha closed this as completed in 1d69fb7 Oct 19, 2021
@ralpha
Copy link
Collaborator

ralpha commented Oct 19, 2021

This is indeed a bug. Data<'r> was defined as a String and not a Vec<u8>.
This was a recent addition, so these types are not all fully tested in actual use.
So it is sometimes difficult to know the exact response that is expected.

Also I noted you linked to the docs for v0.7.0-alpha-1, but we have since released v0.8.0-rc.1. And master branch for Rocket, but we rely on v0.5.0-rc-1. Just make sure you also look at the right documentation because a lot has changed in between these versions. 😉

I'm not going to release a new version for this update yet. But you can use the git master branch for your project for now.
This problem is fixed in there.

If you find any more problems, please let us know.

@Nukesor
Copy link
Contributor Author

Nukesor commented Oct 19, 2021

Awesome!

Thanks a lot for the swift response and your tips :)
Luckily, we're already using the master branch in our project :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants