fix(handler): 药品目录按分页契约修正 YPML00001(T-211)
- DrugQuery.PageNo 去 omitempty(pageNo=0 也上送)+ 加 PageSize - handler Drugs 改 pageNo 必填(parseRequiredInt,缺则返回 400)+ 读 pageSize - 补缺 pageNo 返回 400 的测试 - 依 docx;YPML00001 未真实联调,pageNo 是否真必填待厂家样本核对 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+17
-4
@@ -56,14 +56,19 @@ func (h *PublicHandler) Drugs(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
q := r.URL.Query()
|
q := r.URL.Query()
|
||||||
pageNo, ok := parseOptionalInt(w, q.Get("pageNo"), "pageNo")
|
pageNo, ok := parseRequiredInt(w, q.Get("pageNo"), "pageNo") // docx: 药品目录是分页查询,pageNo 必填
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pageSize, ok := parseOptionalInt(w, q.Get("pageSize"), "pageSize")
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, result, err := h.client.QueryDrugs(r.Context(), osi.DrugQuery{
|
_, result, err := h.client.QueryDrugs(r.Context(), osi.DrugQuery{
|
||||||
PageNo: pageNo,
|
PageNo: pageNo,
|
||||||
YPMC: q.Get("ypmc"),
|
PageSize: pageSize,
|
||||||
PYM: q.Get("pym"),
|
YPMC: q.Get("ypmc"),
|
||||||
|
PYM: q.Get("pym"),
|
||||||
})
|
})
|
||||||
writeRawOrError(w, result, err)
|
writeRawOrError(w, result, err)
|
||||||
}
|
}
|
||||||
@@ -82,6 +87,14 @@ func (h *PublicHandler) Orgs(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeRawOrError(w, result, err)
|
writeRawOrError(w, result, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseRequiredInt(w http.ResponseWriter, raw string, name string) (int, bool) {
|
||||||
|
if raw == "" {
|
||||||
|
writeJSONError(w, http.StatusBadRequest, name+" is required")
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return parseOptionalInt(w, raw, name)
|
||||||
|
}
|
||||||
|
|
||||||
func parseOptionalInt(w http.ResponseWriter, raw string, name string) (int, bool) {
|
func parseOptionalInt(w http.ResponseWriter, raw string, name string) (int, bool) {
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
return 0, true
|
return 0, true
|
||||||
|
|||||||
@@ -88,6 +88,16 @@ func TestPublicOrgsReturnsRawResponse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPublicDrugsRequiresPageNo(t *testing.T) {
|
||||||
|
h := NewPublicHandler(nil) // 缺 pageNo 在触达 client 前返回 400(药品目录分页必填)
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?ypmc=测试", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.Drugs(rec, req)
|
||||||
|
if rec.Code != http.StatusBadRequest {
|
||||||
|
t.Fatalf("status = %d, want 400 (pageNo required)", rec.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPublicDictionaryRejectsInvalidPageNo(t *testing.T) {
|
func TestPublicDictionaryRejectsInvalidPageNo(t *testing.T) {
|
||||||
h := NewPublicHandler(nil)
|
h := NewPublicHandler(nil)
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -25,9 +25,10 @@ type Doctor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DrugQuery struct {
|
type DrugQuery struct {
|
||||||
PageNo int `json:"pageNo,omitempty"`
|
PageNo int `json:"pageNo"` // docx 必填,不加 omitempty 以确保 pageNo=0 也上送(避免被平台判缺参)
|
||||||
YPMC string `json:"ypmc,omitempty"`
|
PageSize int `json:"pageSize,omitempty"`
|
||||||
PYM string `json:"pym,omitempty"`
|
YPMC string `json:"ypmc,omitempty"`
|
||||||
|
PYM string `json:"pym,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Drug struct {
|
type Drug struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user