[Go-Server] Add support for DateTime Query Parameters (#16749)

* Add support for DateTime objects in the Path, Query Params, and as a List

* Fix indentation

* Add an exaple that has dateTimes
Move the date parsing into a common util in the routers.go file.

* Fix compilation issue and regen

* Use the `RequiredError` to handle this case

* Only split on a "," and not an extra 'space' after the ",".
This commit is contained in:
Ian Cubbon
2023-10-22 06:07:13 -07:00
committed by GitHub
parent 5f71bb9afa
commit bf9fae641e
14 changed files with 216 additions and 6 deletions

View File

@@ -136,6 +136,24 @@ paths:
type: string
type: array
style: form
- description: Find pets born after this date
explode: false
in: query
name: bornAfter
required: true
schema:
format: date-time
type: string
style: form
- description: Find pets born before this date
explode: false
in: query
name: bornBefore
required: false
schema:
format: date-time
type: string
style: form
responses:
"200":
content:

View File

@@ -12,6 +12,7 @@ package petstoreserver
import (
"context"
"net/http"
"time"
"os"
)
@@ -68,7 +69,7 @@ type PetAPIServicer interface {
FilterPetsByCategory(context.Context, Gender, Species, []Species) (ImplResponse, error)
FindPetsByStatus(context.Context, []string) (ImplResponse, error)
// Deprecated
FindPetsByTags(context.Context, []string) (ImplResponse, error)
FindPetsByTags(context.Context, []string, time.Time, time.Time) (ImplResponse, error)
GetPetById(context.Context, int64) (ImplResponse, error)
GetPetImageById(context.Context, int64) (ImplResponse, error)
UpdatePet(context.Context, Pet) (ImplResponse, error)

View File

@@ -223,7 +223,21 @@ func (c *PetAPIController) FindPetsByTags(w http.ResponseWriter, r *http.Request
if query.Has("tags") {
tagsParam = strings.Split(query.Get("tags"), ",")
}
result, err := c.service.FindPetsByTags(r.Context(), tagsParam)
if !query.Has("bornAfter"){
c.errorHandler(w, r, &RequiredError{"bornAfter"}, nil)
return
}
bornAfterParam, err := parseTime(query.Get("bornAfter"))
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
bornBeforeParam, err := parseTime(query.Get("bornBefore"))
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.FindPetsByTags(r.Context(), tagsParam, bornAfterParam, bornBeforeParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)

View File

@@ -13,6 +13,7 @@ import (
"context"
"net/http"
"errors"
"time"
"os"
)
@@ -82,7 +83,7 @@ func (s *PetAPIService) FindPetsByStatus(ctx context.Context, status []string) (
// FindPetsByTags - Finds Pets by tags
// Deprecated
func (s *PetAPIService) FindPetsByTags(ctx context.Context, tags []string) (ImplResponse, error) {
func (s *PetAPIService) FindPetsByTags(ctx context.Context, tags []string, bornAfter time.Time, bornBefore time.Time) (ImplResponse, error) {
// TODO - update FindPetsByTags with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

View File

@@ -12,6 +12,7 @@ package petstoreserver
import (
"encoding/json"
"errors"
"time"
"github.com/gorilla/mux"
"io"
"mime/multipart"
@@ -154,6 +155,27 @@ func readFileHeaderToTempFile(fileHeader *multipart.FileHeader) (*os.File, error
return file, nil
}
func parseTimes(param string) ([]time.Time, error) {
splits := strings.Split(param, ",")
times := make([]time.Time, 0, len(splits))
for _, v := range splits {
t, err := parseTime(v)
if err != nil {
return nil, err
}
times = append(times, t)
}
return times, nil
}
// parseTime will parses a string parameter into a time.Time using the RFC3339 format
func parseTime(param string) (time.Time, error) {
if param == "" {
return time.Time{}, nil
}
return time.Parse(time.RFC3339, param)
}
type Number interface {
~int32 | ~int64 | ~float32 | ~float64
}