Skip to content

Commit c3b64c3

Browse files
committed
fix(openapi): support 201 for create
The AEPs specify 201 is the correct response for a successful create request. OAS files which adhere to that were missing their create methods.
1 parent 9862088 commit c3b64c3

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

pkg/api/api.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,22 @@ func GetAPI(api *openapi.OpenAPI, serverURL, pathPrefix string) (*API, error) {
136136
} else {
137137
// create method
138138
if pathItem.Post != nil {
139-
// check if there is a query parameter "id"
140-
lroDetails = pathItem.Post.XAEPLongRunningOperation
141-
if resp, ok := pathItem.Post.Responses["200"]; ok {
142-
sRef = api.GetSchemaFromResponse(resp, openapi.APPLICATION_JSON)
143-
supportsUserSettableCreate := false
144-
for _, param := range pathItem.Post.Parameters {
145-
if param.Name == "id" {
146-
supportsUserSettableCreate = true
147-
break
139+
for _, statusCode := range []string{"200", "201"} {
140+
// check if there is a query parameter "id"
141+
if resp, ok := pathItem.Post.Responses[statusCode]; ok {
142+
lroDetails = pathItem.Post.XAEPLongRunningOperation
143+
sRef = api.GetSchemaFromResponse(resp, openapi.APPLICATION_JSON)
144+
supportsUserSettableCreate := false
145+
for _, param := range pathItem.Post.Parameters {
146+
if param.Name == "id" {
147+
supportsUserSettableCreate = true
148+
break
149+
}
150+
}
151+
r.Methods.Create = &CreateMethod{
152+
SupportsUserSettableCreate: supportsUserSettableCreate,
153+
IsLongRunning: lroDetails != nil,
148154
}
149-
}
150-
r.Methods.Create = &CreateMethod{
151-
SupportsUserSettableCreate: supportsUserSettableCreate,
152-
IsLongRunning: lroDetails != nil,
153155
}
154156
}
155157
}

pkg/api/api_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,42 @@ func TestGetAPI(t *testing.T) {
414414
assert.Equal(t, "https://example.com", sd.Contact.URL)
415415
},
416416
},
417+
{
418+
name: "resource with create returning 201",
419+
api: &openapi.OpenAPI{
420+
OpenAPI: "3.1.0",
421+
Servers: []openapi.Server{{URL: "https://api.example.com"}},
422+
Paths: map[string]*openapi.PathItem{
423+
"/widgets": {
424+
Post: &openapi.Operation{
425+
Responses: map[string]openapi.Response{
426+
"201": {
427+
Content: map[string]openapi.MediaType{
428+
"application/json": {
429+
Schema: &openapi.Schema{
430+
Ref: "#/components/schemas/Widget",
431+
},
432+
},
433+
},
434+
},
435+
},
436+
},
437+
},
438+
},
439+
Components: openapi.Components{
440+
Schemas: map[string]openapi.Schema{
441+
"Widget": {
442+
Type: "object",
443+
},
444+
},
445+
},
446+
},
447+
validateResult: func(t *testing.T, sd *API) {
448+
widget, ok := sd.Resources["widget"]
449+
assert.True(t, ok, "widget resource should exist")
450+
assert.NotNil(t, widget.Methods.Create, "should have CREATE method (from 201 response)")
451+
},
452+
},
417453
{
418454
name: "resource with long running operations",
419455
api: &openapi.OpenAPI{

0 commit comments

Comments
 (0)