swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Body parameter content type xml does not handle nested definition correctly

Open rick22sun opened this issue 10 years ago • 13 comments

When JSON body schema turns into XML, swagger-ui uses tag like "inline_model_0" as in this example: <Model> <inline_model_0> ... </inline_model_0> </Model> This happens when definition has nested objects like:

  "Model": {
    "type": "object",
    "properties": {
      "group1": {
        "type": "object",
        "properties": {
          "param": {
            "type": "string"
          }
        }
      }
    }
  }

For this example, the nested object name "group1" should be used, rather than "inline_model_0".

rick22sun avatar Jan 21 '16 15:01 rick22sun

It is great to see this has been fixed in the latest master. Just wonder if there is a way to customize the top level tag in the results. For example, the above "Model" in JSON has been converted into <?xml version="1.0"?> "<Model> <group1> ... </group1> </Model> This is good. If I have a "Model2" definition in JSON, then the top level XML tag will be <Model2>.

The desired result is to convert both Model and Model2 to the same top level tag (say <input>), i.e., instead of <Model> <group1> ... </group1> </Model> and <Model2> <group1> ... </group1> </Model2>, the desired results will be:

<input xmlns="http://test1.com">
  <group1 xmlns="http://test2.com">
 ...
  <group1>
</input>

Is there a way to do this?

Is it possible to exclude the first line <?xml version="1.0"?> from the result?

rick22sun avatar Jan 28 '16 18:01 rick22sun

@rick22sun If you would like to rename the model, you may add xml.name property to it, please see the example below

  "Model": {
    "type": "object",
    "properties": {
      "group1": {
        "type": "object",
        "properties": {
          "param": {
            "type": "string"
          }
        }
      }
    },
  "xml": {
    "name": "input"
   }
  }

Here is more information about xml object.

Is it possible to exclude the first line from the result?

@webron what do you think about this?

bodnia avatar Jan 28 '16 20:01 bodnia

This is great. "xml" "name" and "namespace" are exactly what I need. On the 2nd question of omitting the first line from the result: I just found out it is OK to keep the line, so please ignore this question for now. Thanks a lot.

rick22sun avatar Jan 28 '16 21:01 rick22sun

With above "Model" definition, got the expected:

<?xml version="1.0"?>
<input>
  <group1>
    <param>string</param>
  </group1>
</input>

In some cases, I need to convert the given "Model" into this:

<?xml version="1.0"?>
<group1>
  <param>string</param>
</group1>

i.e., without the top level tag corresponding to the "Model". Is this possible?

rick22sun avatar Jan 29 '16 21:01 rick22sun

@rick22sun then you may create a separate definition with object nested into model, something like:

        "Model": {
        "type": "object",
        "properties": {
            "group1": {
                "$ref":"#/definitions/Group"
            }
        },
        "xml": {
            "name": "input"
        }
    },
    "Group": {
        "type": "object",
        "properties": {
            "param": {
                "type": "string"
            }
        }
    }

Now Group can be used without Model when needed.

@webron is there any property like xml : { wrapped: true } for arrays but for objects?

bodnia avatar Jan 30 '16 22:01 bodnia

There isn't as it doesn't make a lot of sense. Arrays can be represented in multiple ways in XML. That's not the case for objects.

webron avatar Jan 30 '16 22:01 webron

bodnia, it is clear the idea is to use a nested model definition. However, I can't make it work. In your example, "Group" is at same level as "Model", so if I use "Group" as my definition, then I'll see a <Group> tag in the XML. If I put "Group" inside "Model" and use "Group" for my definition, then nothing will show up in swagger-ui. The need is when convert JSON to XML, do not include any tag for the top level (usually the top level XML tag corresponds to the Model name).

rick22sun avatar Jan 31 '16 02:01 rick22sun

@webron Could you please clarify if title in schema object may re-declare the parent node name?

Example

"schema" : {
    "$ref":"#/definitions/Group",
    "title": "test"
}

If so, then this may be first priority of node name, and only then it's going to be declared in xml object name. And at the same time "title" looks like a field which represents only information sense, like "description". Does this makes sense?

@rick22sun you may add xml.name to definition, to set needed name, but then it appears everywhere this definition is called.

"Group": {
        "type": "object",
        "properties": {
            "param": {
                "type": "string"
            }
        },
        "xml" : {
            "name" : "input"
        }
    }

bodnia avatar Feb 01 '16 09:02 bodnia

It will not. Any properties that exist with a $ref are ignored by definition - that's just how JSON References work.

webron avatar Feb 01 '16 19:02 webron

@rick22sun so there are only 2 options: add just an inline schema object with specific name in xml object. And the second option is to add the same schema into definitions with different names in xml object. @webron could you please confirm this, and maybe propose more options if there are any?

bodnia avatar Feb 03 '16 10:02 bodnia

@bodnia Thanks a lot.

rick22sun avatar Feb 03 '16 15:02 rick22sun

That sounds right. @rick22sun what you're describing in the example above are pretty much two distinct models. You can define your input model, have it refer to group1, and then reference input or group1 based on your requirements.

webron avatar Feb 03 '16 23:02 webron

@webron it looks like this ticket can be closed

bodnia avatar Jun 13 '16 20:06 bodnia