Spec form metadata is lost on visiting it during Swagger UI generation
まだ誰も着手していません。
評価
調査の方向性
spec-tools.visitor/visit と spec-tools.impl/extract-form から始め、次に :return と :body におけるカスタム spec の Swagger UI 生成パスに対して、提供された再現手順を実行します。抽出された form のメタデータを、動作している s/coll-of のケースと比較します。完了の条件は、期待されるメタデータがカスタム visitor から引き続き利用できることです。
索引モデルが issue の本文から書いたものです。
説明
Library Version(s)
2.0.0-alpha29
Problem
I need to redefine spec-tools.visitor/visit for my custom spec to change how spec is shown in Swagger UI. E.g. I have closed-keys custom spec, but want to visit it as s/keys, so that Swagger UI generates appropriate model examples from my spec.
Now, it can be cumbersome to parse the custom spec form to implement its visiting. So I want instead to rely on the metadata attached to the spec form (such easy to use metadata can be conveniently generated by closed-keys macro). I.e.:
(defmethod st-visitor/visit-spec `closed-keys
[spec accept options]
(let [form (st-impl/extract-form spec)
keys-form <generate from (meta form)>]
(st-visitor/visit-spec keys-form accept options)))
But this currently doesn't work because (meta form) is always nil in this function.
I only managed to trace it back to spec-tools.visitor/visit function: it seems to get already crooked spec with no metadata in its form.
The issue is reproducible for custom specs used in :return and :body. I didn't test other places.
Steps:
- Code:
(ns app.foo.handler
(:require [compojure.api.sweet :as c]
[ring.util.http-response :as r]
[clojure.spec.alpha :as s]
[spec-tools.visitor :as st-visitor]
[spec-tools.impl :as st-impl]))
; Helper to redefine spec form
(defn -with-form
[spec form]
{:pre [(s/spec? spec)]}
(reify s/Spec
(describe* [_] form)
; Do not modify other methods
(conform* [_ x] (s/conform* spec x))
(unform* [_ y] (s/unform* spec y))
(explain* [_ path via in x] (s/explain* spec path via in x))
(gen* [_ overrides path rmap] (s/gen* spec overrides path rmap))
(with-gen* [_ gfn] (s/with-gen* spec gfn))))
; Create spec with a custom form and metadata attached to the form
(s/def ::my-spec (-> (s/spec int?)
(-with-form
(with-meta (list 'my-spec 1 2 3) {:my-spec-form-meta [1 2 3]}))))
; Custom visiting which relies on metadata from the form
(defmethod st-visitor/visit-spec 'my-spec
[spec _accept _options]
(let [form (st-impl/extract-form spec)]
(prn :VISITED-FORM form :META (meta form))
nil))
; Handler code
(c/context "/my" []
(c/POST "/foo" []
:return ::my-spec
;:body [x ::my-spec]
(r/ok)))
- Run it and navigate to Swagger UI URL.
- Check console output.
Expected:
:VISITED-FORM (my-spec 1 2 3) :META {:my-spec-form-meta [1 2 3]}
Actual:
:VISITED-FORM (my-spec 1 2 3) :META nil
Workaround:
My current workaround is to attach metadata to the first symbol in the form instead of the whole form. Also in another case I just parse the form instead of relying on its metadata.
Interestingly, the issue is not reproducible when custom spec is "wrapped" by some other spec, specifically (s/coll-of ::my-spec) doesn't seem to have a problem.
- 主要言語
- Clojure
- スター
- 1.1k
- フォーク
- 145
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
metosin/compojure-api のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
metosin/compojure-api#489 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
metosin/compojure-api#468 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
metosin/compojure-api#467 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
metosin/compojure-api#464 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
metosin/compojure-api#463 · コメント 3 件 ·
metosin/compojure-api の issue をすべて見る
似ている issue
-
.Needs Triage .Team/Gadget Priority:P3 Type:Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
metabase/metabase#84431 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
module:db module:terminology-service-local refactoring
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
good first issue guide hacktoberfest
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
TanbirRamim/open-source-radar#138 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
NoahTheDuke/splint#56 ·