Skip to content

[REQ] Add xml support to python client #1715

@spacether

Description

@spacether

Is your feature request related to a problem? Please describe.

The python client does not generate xml code for models defined in a json/yaml swagger/openapi spec.

The python client appears to be missing xml serialization and may be missing deserialization.

When one autogenerates a python client for an endpoint that produces and consumes
application/xml, upon using the python client for that endpoint, the library throws the error:

Cannot prepare a request message for provided
arguments. Please check that your arguments match
declared content type.

in rest.py:RESTClientObject:request
Checking the sent data shows that the correct content headers are being sent, but that the body is still a dict.
The code implies that previous serialization should have been done (perhaps based on content-type)?
But the serialization was not done. So XML should be sent but does not exist yet and is not being sent.

openapi-generator version

3.3.4

Swagger declaration file content or url
swagger: "2.0"
info:
  title: "carrier api"
  description: "An API to connect to the carrier"
  version: "1.0.0"
host: "mailcarrier.com"
basePath: "/api"
schemes:
# - "https"
- "http"
paths:
  /tracking:
    post:
      summary: "Gets tracking info for a single tracking_id"
      description: "Gets tracking info for a single tracking_id"
      operationId: "getTrackingInfo"
      consumes:
      - "text/xml; charset=utf-8"
      produces:
      - "text/xml; charset=utf-8"
      parameters:
        - in: header
          name: SOAPAction
          type: string
          required: true
          default: 'http://mailcarrier.com/IDhlWebApi/GetTrackingInfo'
        - in: body
          name: TrackingRequest
          description: "Tracking Request Body"
          required: true
          schema:
            $ref: "#/definitions/TrackingRequest"
      responses:
        200:
          description: "Valid input"

definitions:
  TrackingRequest:
    xml:
      name: 'Envelope'
      prefix: 'soap-env'
      namespace: 'http://schemas.xmlsoap.org/soap/envelope/'
    required:
      - Header
      - Body
    properties:
      Header:
        type: object
        xml:
          name: 'soap-env:Header'
        required:
          - xmlns:wsa
          - Action
          - MessageID
          - To
        properties:
          xmlns:wsa:
            type: string
            default: 'http://www.w3.org/2005/08/addressing'
            xml:
              attribute: true
          Action:
            type: string
            default: 'http://mailcarrier.com/IDhlWebApi/GetTrackingInfo'
            xml:
              prefix: 'wsa'
          MessageID:
            type: string
            default: 'urn:uuid:123'
            xml:
              prefix: 'wsa'
          To:
            type: string
            default: 'http://mailcarrier.com/api/DhlApi.svc'
            xml:
              prefix: 'wsa'
      Body:
        type: object
        xml:
          prefix: 'soap-env'
        required:
          - GetTrackingInfo
        properties:
          GetTrackingInfo:
            type: object
            xml:
              prefix: 'ns0'
              namespace: 'http://mailcarrier.com/'
            required:
              - request
            properties:
              request:
                type: string
                default: ''
                xml:
                  prefix: 'ns0'
Command line used for generation

Generate the carrierapi library

openapi-generator generate -i carrierapi.yaml -l python -o ./carrierapi -DpackageName=carrierapi

Sample python code sample using the client:

import carrierapi
from carrierapi.rest import ApiException

# create an instance of the API class
api_instance = carrierapi.DefaultApi(carrierapi.ApiClient())
soap_action = 'http://mailcarrier.com/IDhlWebApi/GetTrackingInfo'
tracking_request_header = carrierapi.TrackingRequestHeader(message_id='urn:uuid:123')
get_tracking_info = carrierapi.TrackingRequestBodyGetTrackingInfo()
tracking_request_body = carrierapi.TrackingRequestBody(get_tracking_info=get_tracking_info)
tracking_request = carrierapi.TrackingRequest(header=tracking_request_header, body=tracking_request_body)

try:
    # Gets tracking info for a single tracking_id
    api_instance.get_tracking_info(soap_action, tracking_request)
except ApiException as e:
    print("Exception when calling DefaultApi->get_tracking_info: %s\n" % e)

Describe the solution you'd like

I would like a xml serializer/deserializer added to the python client

Describe alternatives you've considered

Right now, one can convert an object instance to a string body, and send through the client.
The https://pypi.org/project/dicttoxml/ project will not work for my use case because my xml
requires prefixes, namespaces, and attributes, which dicto

Additional context

This feature is also missing from swagger-codegen: swagger-api/swagger-codegen#8995

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions