Skip to content

Commit 22855a0

Browse files
committed
fix: match _id for path params
path params missed an _id suffix, which causes invalid OAS documentation.
1 parent df2333c commit 22855a0

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

pkg/api/openapi.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,6 @@ func generateParentPatternsWithParams(r *Resource) (string, *[]PathWithParams) {
492492
pElem := r.patternElems[i+1]
493493
// Extract the parameter name without the _id suffix
494494
paramName := pElem[1 : len(pElem)-1]
495-
if strings.HasSuffix(paramName, "_id") {
496-
paramName = paramName[:len(paramName)-3]
497-
}
498495
params = append(params, openapi.Parameter{
499496
In: "path",
500497
Name: paramName,
@@ -518,11 +515,11 @@ func generateParentPatternsWithParams(r *Resource) (string, *[]PathWithParams) {
518515
for _, parent := range r.ParentResources() {
519516
singular := parent.Singular
520517
// Convert kebab-case singular to snake_case for path variables
521-
singularSnake := cases.KebabToSnakeCase(singular)
522-
basePattern := fmt.Sprintf("/%s/{%s_id}", CollectionName(parent), singularSnake)
518+
singularSnakeParam := cases.KebabToSnakeCase(singular) + "_id"
519+
basePattern := fmt.Sprintf("/%s/{%s}", CollectionName(parent), singularSnakeParam)
523520
baseParam := openapi.Parameter{
524521
In: "path",
525-
Name: singularSnake,
522+
Name: singularSnakeParam,
526523
Required: true,
527524
Schema: &openapi.Schema{
528525
Type: "string",

pkg/api/openapi_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func TestGenerateParentPatternsWithParams(t *testing.T) {
316316
Params: []openapi.Parameter{
317317
{
318318
In: "path",
319-
Name: "database",
319+
Name: "database_id",
320320
Required: true,
321321
Schema: &openapi.Schema{
322322
Type: "string",
@@ -363,7 +363,7 @@ func TestGenerateParentPatternsWithParams(t *testing.T) {
363363
Params: []openapi.Parameter{
364364
{
365365
In: "path",
366-
Name: "database",
366+
Name: "database_id",
367367
Required: true,
368368
Schema: &openapi.Schema{
369369
Type: "string",
@@ -401,7 +401,7 @@ func TestGenerateParentPatternsWithParams(t *testing.T) {
401401
Params: []openapi.Parameter{
402402
{
403403
In: "path",
404-
Name: "account",
404+
Name: "account_id",
405405
Required: true,
406406
Schema: &openapi.Schema{
407407
Type: "string",
@@ -412,7 +412,7 @@ func TestGenerateParentPatternsWithParams(t *testing.T) {
412412
},
413413
{
414414
In: "path",
415-
Name: "database",
415+
Name: "database_id",
416416
Required: true,
417417
Schema: &openapi.Schema{
418418
Type: "string",

pkg/api/resource.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import (
99
)
1010

1111
type Resource struct {
12+
// the resource singular name is the name of the resource
13+
// in the API. It is used to generate the pattern for the resource.
14+
// e.g: book-edition -> book-editions
15+
//
16+
// The singular name is also used to generate the schema name.
17+
// e.g: book-edition -> BookEdition
18+
//
19+
// the resource singular **must** be in kebab-case.
1220
Singular string `json:"singular"`
1321
Plural string `json:"plural"`
1422
Parents []string `json:"parents,omitempty"`

0 commit comments

Comments
 (0)