Swagger XML 示例值不遵守类上的 XML 注释

问题描述 投票:0回答:1

给定这门课:

    [XmlRoot("MySample", Namespace = "http://mynamespace.org")]
    public class Sample
    {
        [XmlAttribute("version")]
        public string Version { get; set; }

        [XmlAttribute("timestamp")]
        public string TimeStamp{ get; set; }

        [XmlElement("Property1")]
        public string MyProperty { get; set; }
    }

我从我的操作中正确收到以下 XML:

<MySample version="1.2" timestamp="4/12/2024 5:56 PM" xmlns="http://mynamespace.org">
  <Property1>My Value</Property1>
</MySample>

但是,swagger UI 坚持将 XML 示例显示为:

<Sample>
    <Version>string</Version>
    <TimeStamp>string</TimeStamp>
    <MyProperty>string</MyProperty>
</Sample>

有没有什么技巧可以让 Swagger UI 遵循 XML 序列化规则?

xml asp.net-core swagger swashbuckle.aspnetcore
1个回答
0
投票

试试这个。在

XmlAttribute
类中添加
MySample
,然后添加
XmlElement


[XmlRoot(Namespace="http://mynamespace.org")]

public class MySample
{
    [XmlAttribute("version")]
    public string Version { get; set; }

    [XmlAttribute("timestamp")]
    public string TimeStamp{ get; set; }

    [XmlElement("Property1")]
        public Property1 Property1 { get; set; }
}

public class Property1
{
    [XmlText]
        public string prop1Value { get; set; }
}

应该输出这个:

<MySample version="1.0" timestamp="2013-01-01T00:00:00" xmlns="http://mynamespace.org">
    <Property1>Value</Property1>
</MySample>

和 OAS 模式

components:
  schemas:
    MySample:
      type: object
      xml:
        name: MySample
        namespace: http://mynamespace.org
      properties:
      version:
        type: string
        xml:
          name: version
          attribute: true
      timestamp:
        type: string
        xml:
          name: timestamp
          attribute: true      
      Property1:
        type: string
        xml:
          name: Property1
© www.soinside.com 2019 - 2024. All rights reserved.