Skip to content

Commit 8e66294

Browse files
authored
[python-experimental] Enhance octet-stream deserialize (#13402)
* [python-experimental] Enhance octet-stream deserialization When the headers didn't provide the filename, use the url of response to extract filename. * [python-experimental] Remove todo comment. * [python-experimental] Fix test code. * Update samples * [python-experimental] Refined the method and the test + Early return when the url is empty or `None`. + Removed unused f-string prefix. * [python-experimental] Comapre url is None explicitly. * Update samples.
1 parent 194d421 commit 8e66294

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+151
-67
lines changed

modules/openapi-generator/src/main/resources/python-experimental/api_client.handlebars

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import tempfile
1515
import typing
1616
import urllib3
1717
from urllib3._collections import HTTPHeaderDict
18-
from urllib.parse import quote
18+
from urllib.parse import urlparse, quote
1919
from urllib3.fields import RequestField as RequestFieldBase
2020

2121
{{#if tornado}}
@@ -832,6 +832,19 @@ class OpenApiResponse(JSONDetector):
832832
# python must be >= 3.9 so we can pass in bytes into json.loads
833833
return json.loads(response.data)
834834

835+
@staticmethod
836+
def __file_name_from_response_url(response_url: typing.Optional[str]) -> typing.Optional[str]:
837+
if response_url is None:
838+
return None
839+
url_path = urlparse(response_url).path
840+
if url_path:
841+
path_basename = os.path.basename(url_path)
842+
if path_basename:
843+
_filename, ext = os.path.splitext(path_basename)
844+
if ext:
845+
return path_basename
846+
return None
847+
835848
@classmethod
836849
def __file_name_from_content_disposition(cls, content_disposition: typing.Optional[str]) -> typing.Optional[str]:
837850
if content_disposition is None:
@@ -851,13 +864,16 @@ class OpenApiResponse(JSONDetector):
851864
a file will be written and returned
852865
"""
853866
if response.supports_chunked_reads():
854-
file_name = self.__file_name_from_content_disposition(response.headers.get('content-disposition'))
867+
file_name = (
868+
self.__file_name_from_content_disposition(response.headers.get('content-disposition'))
869+
or self.__file_name_from_response_url(response.geturl())
870+
)
855871

856872
if file_name is None:
857873
_fd, path = tempfile.mkstemp()
858874
else:
859875
path = os.path.join(tempfile.gettempdir(), file_name)
860-
# TODO get file_name from the filename at the end of the url if it exists
876+
861877
with open(path, 'wb') as new_file:
862878
chunk_size = 1024
863879
while True:

samples/client/petstore/elixir/lib/openapi_petstore/api/another_fake.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Api.AnotherFake do

samples/client/petstore/elixir/lib/openapi_petstore/api/default.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Api.Default do

samples/client/petstore/elixir/lib/openapi_petstore/api/fake.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Api.Fake do

samples/client/petstore/elixir/lib/openapi_petstore/api/fake_classname_tags123.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Api.FakeClassnameTags123 do

samples/client/petstore/elixir/lib/openapi_petstore/api/pet.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Api.Pet do

samples/client/petstore/elixir/lib/openapi_petstore/api/store.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Api.Store do

samples/client/petstore/elixir/lib/openapi_petstore/api/user.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Api.User do

samples/client/petstore/elixir/lib/openapi_petstore/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Connection do

samples/client/petstore/elixir/lib/openapi_petstore/deserializer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: This file is auto generated by OpenAPI Generator 6.1.0-SNAPSHOT (https://openapi-generator.tech).
1+
# NOTE: This file is auto generated by OpenAPI Generator 6.1.1-SNAPSHOT (https://openapi-generator.tech).
22
# Do not edit this file manually.
33

44
defmodule OpenapiPetstore.Deserializer do

0 commit comments

Comments
 (0)