Skip to content
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

Weos 1251 #65

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ paths:
type: array
items:
$ref: "#/components/schemas/Blog"
delete:
operationId: Delete Blog
parameters:
- in: query
name: id
schema:
type: string
required: true
description: blog id
requestBody:
description: Blog info that is submitted
required: false
content:
application/json:
schema:
$ref: "#/components/schemas/Blog"
responses:
200:
description: Update blog
content:
application/json:
schema:
$ref: "#/components/schemas/Blog"
400:
description: Invalid blog submitted

/blogs/{id}:
get:
parameters:
Expand Down Expand Up @@ -280,6 +306,13 @@ paths:
type: string
required: true
description: blog id
requestBody:
description: Blog info that is submitted
required: false
content:
application/json:
schema:
$ref: "#/components/schemas/Blog"
summary: Delete blog
operationId: Delete Blog
responses:
Expand Down
25 changes: 25 additions & 0 deletions controllers/rest/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,28 @@ func TestRESTAPI_Initialize_ListAddedToGet(t *testing.T) {
}
os.Remove("test.db")
}

func TestRESTAPI_Initialize_DeleteAdded(t *testing.T) {
os.Remove("test.db")
e := echo.New()
tapi := api.RESTAPI{}
_, err := api.Initialize(e, &tapi, "./fixtures/blog.yaml")
if err != nil {
t.Fatalf("unexpected error '%s'", err)
}

found := false
method := "DELETE"
path := "/blogs/:id"
middleware := "Delete"
routes := e.Routes()
for _, route := range routes {
if route.Method == method && route.Path == path && strings.Contains(route.Name, middleware) {
found = true
break
}
}
if !found {
t.Errorf("expected to find delete path")
}
}
4 changes: 2 additions & 2 deletions controllers/rest/controller_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ func (c *StandardControllers) List(app model.Service, spec *openapi3.Swagger, pa
}

func (c *StandardControllers) Delete(app model.Service, spec *openapi3.Swagger, path *openapi3.PathItem, operation *openapi3.Operation) echo.HandlerFunc {
return func(context echo.Context) error {
return func(ctxt echo.Context) error {

return nil
return ctxt.JSON(http.StatusOK, "Deleted")
}
}

Expand Down
19 changes: 19 additions & 0 deletions controllers/rest/fixtures/blog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ paths:
type: string
required: true
description: blog id
x-content-type: Blog
summary: Delete blog
operationId: Delete Blog
responses:
Expand Down Expand Up @@ -421,6 +422,12 @@ paths:
schema:
type: string
required: true
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/Post"
summary: Delete post
responses:
200:
Expand Down Expand Up @@ -519,6 +526,12 @@ paths:
schema:
type: string
required: true
requestBody:
required: false
content:
application/json:
schema:
$ref: "#/components/schemas/Category"
summary: Delete category
responses:
200:
Expand Down Expand Up @@ -634,6 +647,12 @@ paths:
type: string
format: email
required: true
requestBody:
required: false
content:
application/json:
schema:
$ref: "#/components/schemas/Author"
summary: Delete author
responses:
200:
Expand Down
78 changes: 78 additions & 0 deletions controllers/rest/middleware_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,84 @@ func AddStandardController(e *echo.Echo, pathData *openapi3.PathItem, method str

}
}
case "DELETE":
var strContentType string
allParam := true
contentTypeExt := pathData.Delete.ExtensionProps.Extensions[ContentTypeExtension]

if pathData.Delete.RequestBody == nil && contentTypeExt == nil {
break
}

var identifiers []string
var contextName string
var identifierExtension interface{}

if contentTypeExt != nil {
jsonContentType := contentTypeExt.(json.RawMessage)
err := json.Unmarshal(jsonContentType, &strContentType)
if err != nil {
return autoConfigure, nil
}
identifierExtension = swagger.Components.Schemas[strContentType].Value.ExtensionProps.Extensions[IdentifierExtension]
} else {
//check to see if the path can be autoconfigured. If not show a warning to the developer is made aware
for _, value := range pathData.Delete.RequestBody.Value.Content {
if !strings.Contains(value.Schema.Ref, "#/components/schemas/") {
return autoConfigure, nil
}
identifierExtension = swagger.Components.Schemas[strings.Replace(value.Schema.Ref, "#/components/schemas/", "", -1)].Value.ExtensionProps.Extensions[IdentifierExtension]
break
}
}

if identifierExtension != nil {
bytesId := identifierExtension.(json.RawMessage)
json.Unmarshal(bytesId, &identifiers)
}
//check for identifiers
if identifiers != nil && len(identifiers) > 0 {
for _, identifier := range identifiers {
foundIdentifier := false
//check the parameters for the identifiers
for _, param := range pathData.Delete.Parameters {
cName := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if identifier == param.Value.Name || (cName != nil && identifier == cName.(string)) {
foundIdentifier = true
break
}
}
if !foundIdentifier {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
if allParam {
operationConfig.Handler = "Delete"
autoConfigure = true
break
}
}
//if there is no identifiers then id is the default identifier
for _, param := range pathData.Delete.Parameters {

if "id" == param.Value.Name {
operationConfig.Handler = "Delete"
autoConfigure = true
break
}
interfaceContext := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if interfaceContext != nil {
bytesContext := interfaceContext.(json.RawMessage)
json.Unmarshal(bytesContext, &contextName)
if "id" == contextName {
operationConfig.Handler = "Delete"
autoConfigure = true
break
}
}
}
}

return autoConfigure, nil
Expand Down
3 changes: 3 additions & 0 deletions controllers/rest/openapi_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ const IdentifierExtension = "x-identifier"

//AliasExtension alias parameter name to a different name in the controller
const AliasExtension = "x-alias"

//ContentTypeExtension alias for specifying the content type instead of the request body
const ContentTypeExtension = "x-content-type"
1 change: 0 additions & 1 deletion end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,6 @@ func TestBDD(t *testing.T) {
Options: &godog.Options{
Format: "pretty",
Tags: "~skipped && ~long",
//Tags: "long",
},
}.Run()
if status != 0 {
Expand Down
Loading