oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Full XML support not implemented

Open matyat opened this issue 4 years ago • 2 comments

Handling XML responses and requests is severely limited in the generated code as the full OpenAPI spec WRT XML is not fully implemented.

Given this example API definition:

openapi: 3.0.3
info:
  version: 1.0.0
  title: test-api

paths:
  /foo:
    get:
      responses:
        200:
          description: OK
          content:
            application/xml:
              schema:
                $ref: "#/components/schemas/document"

components:
  schemas:
    document:
      type: object
      required:
      - name
      - item
      properties:
        name:
          type: string
        item:
          type: object
          required:
          - type
          - value
          properties:
            type:
              type: string
              xml:
                attribute: true
            value:
              type: string

The following type is generated:

// Package Testapi provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.9.0 DO NOT EDIT.
package Testapi

// Document defines model for document.
type Document struct {
        Item struct {
                Type  string `json:"type"`
                Value string `json:"value"`
        } `json:"item"`
        Name string `json:"name"`
}

However, there are no xml tags present at all. The expected output would be something like:

// Package Testapi provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.9.0 DO NOT EDIT.
package Testapi

import "encoding/xml"

// Document defines model for document.
type Document struct {
        XMLName xml.Name `xml:"document"`
        Item    struct {
                Type  string `json:"type" xml:"type,attr"`
                Value string `json:"value" xml:"value"`
        } `json:"item" xml:"item"`
        Name string `json:"name" xml:"name"`
}

Representing XML in OpenAPI 3: https://swagger.io/docs/specification/data-models/representing-xml/

matyat avatar Nov 23 '21 17:11 matyat

Put together a fairly minimal patch to address this here https://github.com/deepmap/oapi-codegen/pull/488. Let me know if it's of any use :).

matyat avatar Nov 24 '21 11:11 matyat

Hi @matyat, thanks for the patching. I am looking for the same thing. I am testing your patch. Will let you know if there is a problem 😄

kennykarnama avatar Nov 15 '22 03:11 kennykarnama

Are there any news to this?

Giftzwerg02 avatar Aug 05 '24 14:08 Giftzwerg02