API Platform Swagger中传真示例值被转八进制问题求助
问题描述
我用XML配置API Platform的fax字段,配置如下:
<property name="fax" required="false" identifier="false" writable="true" readable="true" description="A fax for the company."> <attribute name="openapi_context"> <attribute name="example">0133333333</attribute> </attribute> </property>
但在Swagger中显示的却是:
"fax": "23967451",
这个值和配置的示例完全不同。我发现删除开头的"0"后,数字"133333333"能正常显示;把0133333333转成八进制后结果正好是23967451。其他包含fax字段的实体都能正常显示示例值,我的实体中fax定义为:
private ?string $fax = null;
Doctrine配置为:
<field name="fax" type="string" column="fax" length="25" nullable="true"/>
而且该字段没有验证规则,请问这是什么原因?
原因分析与解决方案
原因
问题出在XML配置的解析逻辑上:API Platform读取你设置的example值时,把以0开头的0133333333判定为八进制数字,自动转成十进制的23967451后显示在Swagger里。哪怕你的实体和Doctrine都把fax定义成字符串类型,XML解析器还是按数值规则处理了这个开头带0的内容。
解决方案
要让解析器把这个值当成纯字符串看待,给XML里的example值套上CDATA标记即可,明确告诉解析器这是文本内容,不要进行进制转换:
<property name="fax" required="false" identifier="false" writable="true" readable="true" description="A fax for the company."> <attribute name="openapi_context"> <attribute name="example"><![CDATA[0133333333]]></attribute> </attribute> </property>
修改后Swagger就会准确显示0133333333作为示例值了。
内容的提问来源于stack exchange,提问作者tenfifty6
相关产品推荐
相关产品推荐

