From 4bd58789f4bc91ead1756d049755dd23fd6db9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Fri, 13 May 2022 16:12:18 +0200 Subject: [PATCH] fix: server error when passing empty heartbeats slice fix: do not allow to set id for diagnostics inputs chore: remove authentication for diagnostics endpoint from swagger docs --- README.md | 2 +- routes/api/diagnostics.go | 1 - services/diagnostics.go | 1 + services/heartbeat.go | 4 +++ static/docs/docs.go | 75 ++++++++------------------------------- static/docs/swagger.json | 8 ----- static/docs/swagger.yaml | 4 --- 7 files changed, 20 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index baaa66d..c326ae0 100644 --- a/README.md +++ b/README.md @@ -204,7 +204,7 @@ See our [Swagger API Documentation](https://wakapi.dev/swagger-ui). ### Generating Swagger docs ```bash -$ go get -u github.com/swaggo/swag/cmd/swag +$ go install github.com/swaggo/swag/cmd/swag@latest $ swag init -o static/docs ``` diff --git a/routes/api/diagnostics.go b/routes/api/diagnostics.go index f22bf4a..219204d 100644 --- a/routes/api/diagnostics.go +++ b/routes/api/diagnostics.go @@ -36,7 +36,6 @@ func (h *DiagnosticsApiHandler) RegisterRoutes(router *mux.Router) { // @Tags diagnostics // @Accept json // @Param diagnostics body models.Diagnostics true "A single diagnostics object sent by WakaTime CLI" -// @Security ApiKeyAuth // @Success 201 // @Router /plugins/errors [post] func (h *DiagnosticsApiHandler) Post(w http.ResponseWriter, r *http.Request) { diff --git a/services/diagnostics.go b/services/diagnostics.go index 8381dec..59edb79 100644 --- a/services/diagnostics.go +++ b/services/diagnostics.go @@ -19,5 +19,6 @@ func NewDiagnosticsService(diagnosticsRepo repositories.IDiagnosticsRepository) } func (srv *DiagnosticsService) Create(diagnostics *models.Diagnostics) (*models.Diagnostics, error) { + diagnostics.ID = 0 return srv.repository.Insert(diagnostics) } diff --git a/services/heartbeat.go b/services/heartbeat.go index 96b80b4..ec9f61e 100644 --- a/services/heartbeat.go +++ b/services/heartbeat.go @@ -54,6 +54,10 @@ func (srv *HeartbeatService) Insert(heartbeat *models.Heartbeat) error { } func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error { + if len(heartbeats) == 0 { + return nil + } + hashes := datastructure.NewSet[string]() // https://github.com/muety/wakapi/issues/139 diff --git a/static/docs/docs.go b/static/docs/docs.go index b8cd1e2..704c291 100644 --- a/static/docs/docs.go +++ b/static/docs/docs.go @@ -1,22 +1,14 @@ -// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT +// Package docs GENERATED BY SWAG; DO NOT EDIT // This file was generated by swaggo/swag - package docs -import ( - "bytes" - "encoding/json" - "strings" +import "github.com/swaggo/swag" - "github.com/alecthomas/template" - "github.com/swaggo/swag" -) - -var doc = `{ +const docTemplate = `{ "schemes": {{ marshal .Schemes }}, "swagger": "2.0", "info": { - "description": "{{.Description}}", + "description": "{{escape .Description}}", "title": "{{.Title}}", "contact": { "name": "Ferdinand Mütsch", @@ -612,11 +604,6 @@ var doc = `{ }, "/plugins/errors": { "post": { - "security": [ - { - "ApiKeyAuth": [] - } - ], "consumes": [ "application/json" ], @@ -1341,9 +1328,6 @@ var doc = `{ "machine_name_id": { "type": "string" }, - "modified_at": { - "type": "string" - }, "project": { "type": "string" }, @@ -1697,49 +1681,18 @@ var doc = `{ } }` -type swaggerInfo struct { - Version string - Host string - BasePath string - Schemes []string - Title string - Description string -} - // SwaggerInfo holds exported Swagger Info so clients can modify it -var SwaggerInfo = swaggerInfo{ - Version: "1.0", - Host: "", - BasePath: "/api", - Schemes: []string{}, - Title: "Wakapi API", - Description: "REST API to interact with [Wakapi](https://wakapi.dev)\n\n## Authentication\nSet header `Authorization` to your API Key encoded as Base64 and prefixed with `Basic`\n**Example:** `Basic ODY2NDhkNzQtMTljNS00NTJiLWJhMDEtZmIzZWM3MGQ0YzJmCg==`", -} - -type s struct{} - -func (s *s) ReadDoc() string { - sInfo := SwaggerInfo - sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1) - - t, err := template.New("swagger_info").Funcs(template.FuncMap{ - "marshal": func(v interface{}) string { - a, _ := json.Marshal(v) - return string(a) - }, - }).Parse(doc) - if err != nil { - return doc - } - - var tpl bytes.Buffer - if err := t.Execute(&tpl, sInfo); err != nil { - return doc - } - - return tpl.String() +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "", + BasePath: "/api", + Schemes: []string{}, + Title: "Wakapi API", + Description: "REST API to interact with [Wakapi](https://wakapi.dev)\n\n## Authentication\nSet header `Authorization` to your API Key encoded as Base64 and prefixed with `Basic`\n**Example:** `Basic ODY2NDhkNzQtMTljNS00NTJiLWJhMDEtZmIzZWM3MGQ0YzJmCg==`", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, } func init() { - swag.Register(swag.Name, &s{}) + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) } diff --git a/static/docs/swagger.json b/static/docs/swagger.json index ef6672b..17702e6 100644 --- a/static/docs/swagger.json +++ b/static/docs/swagger.json @@ -596,11 +596,6 @@ }, "/plugins/errors": { "post": { - "security": [ - { - "ApiKeyAuth": [] - } - ], "consumes": [ "application/json" ], @@ -1325,9 +1320,6 @@ "machine_name_id": { "type": "string" }, - "modified_at": { - "type": "string" - }, "project": { "type": "string" }, diff --git a/static/docs/swagger.yaml b/static/docs/swagger.yaml index 3202016..7a2c0e5 100644 --- a/static/docs/swagger.yaml +++ b/static/docs/swagger.yaml @@ -166,8 +166,6 @@ definitions: type: string machine_name_id: type: string - modified_at: - type: string project: type: string time: @@ -808,8 +806,6 @@ paths: responses: "201": description: "" - security: - - ApiKeyAuth: [] summary: Push a new diagnostics object tags: - diagnostics