Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions autorest/codegen/models/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from .base_schema import BaseSchema
from .schema_request import SchemaRequest
from .object_schema import ObjectSchema
from .constant_schema import ConstantSchema


_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -105,15 +104,6 @@ def __init__(
def python_name(self) -> str:
return self.name

@property
def request_content_type(self) -> str:
return next(iter(
[
p.schema.get_declaration(cast(ConstantSchema, p.schema).value)
for p in self.parameters.constant if p.serialized_name == "content_type"
]
))

@property
def is_stream_request(self) -> bool:
"""Is the request is a stream, like an upload."""
Expand Down
2 changes: 1 addition & 1 deletion autorest/codegen/models/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(
self.grouped_by = grouped_by
self.original_parameter = original_parameter
self._client_default_value = client_default_value
self.is_kwarg: bool = False
self.is_kwarg: bool = self.rest_api_name == "Content-Type" and self.constant
self.has_multiple_media_types: bool = False
self.multiple_media_types_type_annot: Optional[str] = None
self.multiple_media_types_docstring_type: Optional[str] = None
Expand Down
20 changes: 20 additions & 0 deletions autorest/codegen/models/parameter_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .parameter import Parameter, ParameterLocation
from .object_schema import ObjectSchema
from .constant_schema import ConstantSchema


_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -95,6 +96,25 @@ def constant(self) -> List[Parameter]:
lambda parameter: parameter.constant
)

@property
def content_type_parameter(self) -> Parameter:
try:
content_type_param = next(iter(
[
p for p in self.parameters
if p.rest_api_name == "Content-Type"
]
))
return content_type_param
except StopIteration:
raise ValueError("You are looking for a Content-Type parameter, but there is none for this operation.")

@property
def content_type(self) -> str:
if isinstance(self.content_type_parameter.schema, ConstantSchema):
return self.content_type_parameter.schema.get_declaration(self.content_type_parameter.schema.value)
return self.content_type_parameter.schema.default_value_declaration

@property
def method(self) -> List[Parameter]:
"""The list of parameter used in method signature.
Expand Down
10 changes: 8 additions & 2 deletions autorest/codegen/templates/operation.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@
{% endif %}
{% if operation.parameters.constant|selectattr("implementation", "equalto", "Method")|selectattr("original_parameter", "equalto", None)|selectattr("in_method_code") %}
{% for constant_parameter in operation.parameters.constant|selectattr("implementation", "equalto", "Method")|selectattr("original_parameter", "equalto", None)|selectattr("in_method_code") %}
{% if constant_parameter.serialized_name == "content_type" %}
content_type = kwargs.pop("content_type", {{ constant_parameter.constant_declaration }})
{% if constant_parameter.is_kwarg and constant_parameter.rest_api_name == "Content-Type" %}
{{ constant_parameter.serialized_name }} = kwargs.pop("content_type", {{ constant_parameter.constant_declaration }})
{% else %}
{{ constant_parameter.serialized_name }} = {{ constant_parameter.constant_declaration }}
{% endif %}
{% endfor %}
{% endif %}

{% if operation.parameters.has_body %}
{% set content_type_parameter = operation.parameters.content_type_parameter %}
{% if not content_type_parameter.constant %}
{{ content_type_parameter.serialized_name }} = kwargs.pop("content_type", {{ content_type_parameter.schema.default_value_declaration }})
{% endif %}
{% endif %}
# Construct URL
url = self.{{ operation.python_name }}.metadata['url'] # type: ignore
{% if operation.parameters.path %}
Expand Down
5 changes: 2 additions & 3 deletions autorest/codegen/templates/operation_tools.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ else ((return_type_wrapper | join("[") + "[") if return_type_wrapper else "") ~

{# content type docstring #}
{% macro content_type_docstring(operation) %}
{% set content_type_constant = operation.parameters.constant|selectattr("implementation", "equalto", "Method")|selectattr("original_parameter", "equalto", None)|selectattr("in_method_code") | selectattr("serialized_name", "equalto", "content_type") | first %}
:keyword str content_type: Media type of the body sent to the API. Default value is {{ content_type_constant.constant_declaration }}.
:keyword str content_type: Media type of the body sent to the API. Default value is {{ operation.parameters.content_type }}.
Allowed values are: "{{ operation.requests | map(attribute="media_types") | sum(start = []) | unique | list | join ('", "') }}".{% endmacro %}

{# error map handling #}
Expand Down Expand Up @@ -166,7 +165,7 @@ _form_content = {
{% endfor %}
}
{% else %}
{% set send_xml = "xml" if operation.parameters.has_body and "xml" in operation.request_content_type %}
{% set send_xml = "xml" if operation.parameters.has_body and "xml" in operation.parameters.content_type %}
{% set request_as_xml = ", is_xml=True" if send_xml else "" %}
{% if operation.parameters.has_body %}
{% set body_content_kwargs_signature = ", **body_content_kwargs" %}
Expand Down
2 changes: 1 addition & 1 deletion autorest/codegen/templates/paging_operation.py.jinja2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import 'operation_tools.jinja2' as op_tools %}
{% import 'paging_operation_helper.jinja2' as helper %}
{% set send_xml = "xml" if operation.parameters.has_body and "xml" in operation.request_content_type %}
{% set send_xml = "xml" if operation.parameters.has_body and "xml" in operation.parameters.content_type %}
{% set request_as_xml = ", is_xml=True" if send_xml else "" %}
{% macro return_docstring(async_mode) %}
{% if operation.responses | selectattr('has_body') | first %}
Expand Down