背景如何解決揚鞭錯誤「實例類型(字符串)不符合任何允許的基本類型......」
我剛開始一個新的項目,並想用揚鞭我的API文檔。我目前正在本地運行我的項目,並在IIS中進行託管。
我修改了我的主機文件,爲網站提供了一個有效的標題。對於這篇文章,讓我們說頭是publicapiurl.domain.com。所以,我已經添加下面進入我的hosts文件:
127.0.0.1 publicapiurl.domain.com
現在,當我輸入publicapiurl.domain.com/swagger我得到的招搖文檔。最初的設置看起來很簡單,但我在swagger doc的右下角有一個紅色的'ERROR {...}'信息。該錯誤消息如下:
{"messages":["malformed or unreadable swagger supplied"],"schemaValidationMessages":[{"level":"error","domain":"validation","keyword":"type","message":"instance type (string) does not match any allowed primitive type (allowed: [\"object\"])","schema":{"loadingURI":"#","pointer":""},"instance":{"pointer":""}}]}
我在過去的工作有點用揚鞭,所以我把所提供的鏈接到生成招搖doc和複製的代碼。我將代碼粘貼到swagger.io/tools編輯器中,看看他們的驗證過程可能會告訴我什麼。我粘貼的代碼驗證沒有任何錯誤。下面是代碼:
swagger: '2.0'
info:
version: v1
title: Generic.Public.Api
host: publicapiurl.domain.com
schemes:
- http
paths:
/api/Values:
get:
tags:
- Values
operationId: Values_Get
consumes: []
produces:
- application/json
- text/json
- application/xml
- text/xml
responses:
'200':
description: OK
schema:
type: array
items:
type: string
post:
tags:
- Values
operationId: Values_PostByvalue
consumes:
- application/json
- text/json
- application/xml
- text/xml
- application/x-www-form-urlencoded
produces: []
parameters:
- name: value
in: body
required: true
schema:
type: string
responses:
'204':
description: No Content
'/api/Values/{id}':
get:
tags:
- Values
operationId: Values_GetByid
consumes: []
produces:
- application/json
- text/json
- application/xml
- text/xml
parameters:
- name: id
in: path
required: true
type: integer
format: int32
responses:
'200':
description: OK
schema:
type: string
put:
tags:
- Values
operationId: Values_PutByidvalue
consumes:
- application/json
- text/json
- application/xml
- text/xml
- application/x-www-form-urlencoded
produces: []
parameters:
- name: id
in: path
required: true
type: integer
format: int32
- name: value
in: body
required: true
schema:
type: string
responses:
'204':
description: No Content
delete:
tags:
- Values
operationId: Values_DeleteByid
consumes: []
produces: []
parameters:
- name: id
in: path
required: true
type: integer
format: int32
responses:
'204':
description: No Content
definitions: {}
有誰知道我實際上得到上述錯誤意味着或我怎麼可能能夠解決呢?
我最好的猜測是它與我對hosts文件的修改以及一些類型的CORS問題有關......但我非常茫然。任何建議表示讚賞!
編輯:
我更簡化了控制器和刪除XML響應類型,但我還是接受我的本地IIS運行同樣的錯誤。在swagger在線編輯器中,swagger的定義仍然沒有錯誤地驗證。
我也從Swashbuckle nuget包切換到Swashbuckle.Core,但結果是一樣的。
這是新的昂首闊步的定義:
swagger: '2.0'
info:
version: v1
title: Generic Public Api
host: l-publicapi.generic.com
schemes:
- http
paths:
/api/values/values:
get:
tags:
- Values
operationId: Values_Get
consumes: []
produces:
- application/json
- text/json
responses:
'200':
description: OK
schema:
type: array
items:
type: string
'/api/values/values/{id}':
get:
tags:
- Values
operationId: Values_GetByid
consumes: []
produces:
- application/json
- text/json
parameters:
- name: id
in: path
required: true
type: integer
format: int32
responses:
'200':
description: OK
schema:
type: string
definitions: {}
任何額外的建議?
感謝您的回覆。我將嘗試僅指定JSON並查看是否有任何區別。 – Zoop
我得到了同樣的錯誤,並以這種方式解決它:在Swagger UI中打開你的swagger文件,並將其轉換爲Swagger 3.0。然後將3.0文件上傳到您的託管項目。錯誤「實例類型(字符串)不匹配任何允許的基元類型...」應該消失。 – Peter