clowder-framework / clowder2-frontend-old

Clowder v2 frontend (in development)
Apache License 2.0
0 stars 0 forks source link

File Download response.blob not available from generated client code #47

Open bodom0015 opened 2 years ago

bodom0015 commented 2 years ago

This issue was uncovered during PR review: https://github.com/clowder-framework/clowder2-react-frontend/pull/41#discussion_r769123726

We now generate our OpenAPI client code from the spec produced by FastAPI. With this, we should get a client call that allows us to download a file from Clowder, as described by this endpoint in the spec:

        "/api/v2/files/{file_id}": {
            "get": {
                "tags": ["files"],
                "summary": "Download File",
                "operationId": "download_file_api_v2_files__file_id__get",
                "parameters": [{
                    "required": true,
                    "schema": {
                        "title": "File Id",
                        "type": "string"
                    },
                    "name": "file_id",
                    "in": "path"
                }],
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {}
                            }
                        }
                    },
                    "422": {
                        "description": "Validation Error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/HTTPValidationError"
                                }
                            }
                        }
                    }
                },
                "security": [{
                    "HTTPBearer": []
                }]
            },

Instead we appear to be unable to access response.blob() to fetch the actual file contents, as this is handled at a lower level by the generated code. Looking into it, the handling for this that we see in FastAPI's /docs endpoint appears to be an abstraction that FastAPI has added over the top of OpenAPI:

Screen Shot 2021-12-16 at 8 06 54 PM

My reasoning for this thinking is that the official Swagger UI does not present this spec in the same way:

Screen Shot 2021-12-16 at 8 06 29 PM

OpenAPI does offer a way to specify arbitrary types for response data, but even then I'm not sure if the generator supports it. I still need to experiment to see if this is even possible with the openapi-typescript-codegen generator that we currently use.

TL;DR: there may be a (possibly long) path to getting this implemented using the same codegen pattern we plan to use elsewhere, but ultimately this is not a REST-ful endpoint since it returns binary data. We can make this a lot easier on ourselves by simply making a manual request to that endpoint URL and already have the code that makes this work :+1: ideally if auth were shared between browser tabs, we would just need to open a new tab to that endpoint (see last point below)


Nice to have:

bodom0015 commented 2 years ago

One thing that might alleviate one of the above concerns is that it looks like the official set of generators has been xpanded and now includes a generic typescript-fetch that appears to support Blob responses: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L295-L301

I have not used this variant myself, as I didn't think they offered a generic TS generator (last I checked there were only a couple for different versions of Angular), but I was able to generate without issues with their CLI tool:

clowder2-react-frontend % ./node_modules/.bin/openapi-generator-cli generate \
  -i http://localhost:8888/api/v2/openapi.json \
  -o src/generated-sources/openapi \
  -g typescript-fetch \
  --additional-properties=supportsES6=true,npmVersion=6.9.0,typescriptThreePlus=true
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - Output directory (/Users/lambert8/workspace/syngenta/clowder2-react-frontend/src/generated-sources/openapi) does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: typescript-fetch (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'typescript-fetch' is considered stable.

    ...    ...    ...    ...    ...

[main] INFO  o.o.codegen.TemplateManager - writing file /Users/lambert8/workspace/syngenta/clowder2-react-frontend/src/generated-sources/openapi/.openapi-generator/FILES
################################################################################
# Thanks for using OpenAPI Generator.                                          #
# Please consider donation to help us maintain this project 🙏                 #
# https://opencollective.com/openapi_generator/donate                          #
################################################################################

Could it be worth considering switching to the official typescript-fetch generator if it might have better support for file downloads? I wonder if there are other use case gaps for openapi in the current generator... 🤔