-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
swagger-cli validateがvalidとなるapi.jsonを作れるようにする #12403
Merged
syuilo
merged 11 commits into
misskey-dev:develop
from
samunohito:enhance/valid-openapi-json
Nov 22, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1e36284
api.jsonがswagger-cli validateでエラーにならないように生成ロジックを修正
samunohito 443d4f5
Merge branch 'develop' into enhance/valid-openapi-json
samunohito c3773a3
フィールドの消し方に不備があったので変更
samunohito 61180ad
バックエンドを起動しなくてもapi.jsonを作れるようにした
samunohito 5cce089
deepCopyしてからレスポンス部分を作るようにした
samunohito 02d4a7b
Merge branch 'develop' into enhance/valid-openapi-json
samunohito 2fd0530
fix CHANGELOG.md
samunohito b043aaa
securitySchemesの定義を復活&ApiCallServiceの実装的にベアラトークンなのでその形で
samunohito 819f2a2
bodyが無い(空オブジェクト)のときはrequestBodyを描画しないようにする
samunohito a9cb82e
allowGetがtrueな項目はget用の記載も作成
samunohito f9bf848
Merge branch 'develop' into enhance/valid-openapi-json
syuilo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { loadConfig } from './built/config.js' | ||
import { genOpenapiSpec } from './built/server/api/openapi/gen-spec.js' | ||
import { writeFileSync } from "node:fs"; | ||
|
||
const config = loadConfig(); | ||
const spec = genOpenapiSpec(config); | ||
|
||
writeFileSync('./built/api.json', JSON.stringify(spec), 'utf-8'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
*/ | ||
|
||
import type { Config } from '@/config.js'; | ||
import endpoints from '../endpoints.js'; | ||
import endpoints, { IEndpoint } from '../endpoints.js'; | ||
import { errors as basicErrors } from './errors.js'; | ||
import { schemas, convertSchemaToOpenApiSchema } from './schemas.js'; | ||
|
||
|
@@ -33,16 +33,17 @@ export function genOpenapiSpec(config: Config) { | |
schemas: schemas, | ||
|
||
securitySchemes: { | ||
ApiKeyAuth: { | ||
type: 'apiKey', | ||
in: 'body', | ||
name: 'i', | ||
bearerAuth: { | ||
type: 'http', | ||
scheme: 'bearer', | ||
}, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inには"header"、"query"、"cookie"のみ設定できるとのことでした… |
||
}, | ||
}; | ||
|
||
for (const endpoint of endpoints.filter(ep => !ep.meta.secure)) { | ||
// 書き換えたりするのでディープコピーしておく。そのまま編集するとメモリ上の値が汚れて次回以降の出力に影響する | ||
const copiedEndpoints = JSON.parse(JSON.stringify(endpoints)) as IEndpoint[]; | ||
for (const endpoint of copiedEndpoints.filter(ep => !ep.meta.secure)) { | ||
const errors = {} as any; | ||
|
||
if (endpoint.meta.errors) { | ||
|
@@ -79,6 +80,13 @@ export function genOpenapiSpec(config: Config) { | |
schema.required = [...schema.required ?? [], 'file']; | ||
} | ||
|
||
if (schema.required && schema.required.length <= 0) { | ||
// 空配列は許可されない | ||
schema.required = undefined; | ||
} | ||
|
||
const hasBody = (schema.type === 'object' && schema.properties && Object.keys(schema.properties).length >= 1); | ||
|
||
const info = { | ||
operationId: endpoint.name, | ||
summary: endpoint.name, | ||
|
@@ -92,17 +100,19 @@ export function genOpenapiSpec(config: Config) { | |
} : {}), | ||
...(endpoint.meta.requireCredential ? { | ||
security: [{ | ||
ApiKeyAuth: [], | ||
bearerAuth: [], | ||
}], | ||
} : {}), | ||
requestBody: { | ||
required: true, | ||
content: { | ||
[requestType]: { | ||
schema, | ||
...(hasBody ? { | ||
requestBody: { | ||
required: true, | ||
content: { | ||
[requestType]: { | ||
schema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} : {}), | ||
responses: { | ||
...(endpoint.meta.res ? { | ||
'200': { | ||
|
@@ -118,6 +128,11 @@ export function genOpenapiSpec(config: Config) { | |
description: 'OK (without any results)', | ||
}, | ||
}), | ||
...(endpoint.meta.res?.optional === true || endpoint.meta.res?.nullable === true ? { | ||
'204': { | ||
description: 'OK (without any results)', | ||
}, | ||
} : {}), | ||
'400': { | ||
description: 'Client error', | ||
content: { | ||
|
@@ -190,6 +205,7 @@ export function genOpenapiSpec(config: Config) { | |
}; | ||
|
||
spec.paths['/' + endpoint.name] = { | ||
...(endpoint.meta.allowGet ? { get: info } : {}), | ||
post: info, | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type: 'null'
は定義されない書式らしいです…代わりに、res直下のoptionalまたはnullableをtrueにすると、レスポンスのHTTPステータス一覧に204が生えるようにしています。