From 96da0396fee3238a91a86e608b7341ed53df332b Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Sun, 13 Oct 2024 22:30:10 -0700 Subject: [PATCH] feat(openapi): using resource singular this makes it easier to extract the resource singular for arguments. --- example/bookstore/v1/bookstore_openapi.json | 60 ++++++++++----------- example/bookstore/v1/bookstore_openapi.yaml | 60 ++++++++++----------- writer/openapi/openapi.go | 12 ++--- 3 files changed, 66 insertions(+), 66 deletions(-) diff --git a/example/bookstore/v1/bookstore_openapi.json b/example/bookstore/v1/bookstore_openapi.json index e7281b5..c5107b9 100644 --- a/example/bookstore/v1/bookstore_openapi.json +++ b/example/bookstore/v1/bookstore_openapi.json @@ -67,7 +67,7 @@ ] } }, - "/publishers/{publisher_id}": { + "/publishers/{publisher}": { "delete": { "responses": { "200": { @@ -77,7 +77,7 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" @@ -95,7 +95,7 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" @@ -113,7 +113,7 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" @@ -146,7 +146,7 @@ ] } }, - "/publishers/{publisher_id}/books": { + "/publishers/{publisher}/books": { "get": { "responses": { "200": { @@ -160,7 +160,7 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" @@ -192,7 +192,7 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" @@ -214,7 +214,7 @@ ] } }, - "/publishers/{publisher_id}/books/{book_id}": { + "/publishers/{publisher}/books/{book}": { "delete": { "responses": { "200": { @@ -224,14 +224,14 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_id", + "name": "book", "schema": {}, "required": true, "type": "string" @@ -249,14 +249,14 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_id", + "name": "book", "schema": {}, "required": true, "type": "string" @@ -274,14 +274,14 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_id", + "name": "book", "schema": {}, "required": true, "type": "string" @@ -306,7 +306,7 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" @@ -321,7 +321,7 @@ ] } }, - "/publishers/{publisher_id}/books/{book_id}/editions": { + "/publishers/{publisher}/books/{book}/editions": { "get": { "responses": { "200": { @@ -335,14 +335,14 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_id", + "name": "book", "schema": {}, "required": true, "type": "string" @@ -374,14 +374,14 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_id", + "name": "book", "schema": {}, "required": true, "type": "string" @@ -403,7 +403,7 @@ ] } }, - "/publishers/{publisher_id}/books/{book_id}/editions/{book_edition_id}": { + "/publishers/{publisher}/books/{book}/editions/{book-edition}": { "delete": { "responses": { "200": { @@ -413,21 +413,21 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_id", + "name": "book", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_edition_id", + "name": "book-edition", "schema": {}, "required": true, "type": "string" @@ -445,21 +445,21 @@ "parameters": [ { "in": "path", - "name": "publisher_id", + "name": "publisher", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_id", + "name": "book", "schema": {}, "required": true, "type": "string" }, { "in": "path", - "name": "book_edition_id", + "name": "book-edition", "schema": {}, "required": true, "type": "string" @@ -515,7 +515,7 @@ "singular": "book", "plural": "books", "patterns": [ - "/publishers/{publisher_id}/books/{book_id}" + "/publishers/{publisher}/books/{book}" ], "parents": [ "publisher" @@ -545,7 +545,7 @@ "singular": "book-edition", "plural": "book-editions", "patterns": [ - "/publishers/{publisher_id}/books/{book_id}/editions/{book_edition_id}" + "/publishers/{publisher}/books/{book}/editions/{book-edition}" ], "parents": [ "book" @@ -572,7 +572,7 @@ "singular": "publisher", "plural": "publishers", "patterns": [ - "/publishers/{publisher_id}" + "/publishers/{publisher}" ] } } diff --git a/example/bookstore/v1/bookstore_openapi.yaml b/example/bookstore/v1/bookstore_openapi.yaml index 43e7883..0317fed 100644 --- a/example/bookstore/v1/bookstore_openapi.yaml +++ b/example/bookstore/v1/bookstore_openapi.yaml @@ -34,7 +34,7 @@ components: parents: - publisher patterns: - - /publishers/{publisher_id}/books/{book_id} + - /publishers/{publisher}/books/{book} plural: books singular: book book-edition: @@ -55,7 +55,7 @@ components: parents: - book patterns: - - /publishers/{publisher_id}/books/{book_id}/editions/{book_edition_id} + - /publishers/{publisher}/books/{book}/editions/{book-edition} plural: book-editions singular: book-edition publisher: @@ -72,7 +72,7 @@ components: type: object x-aep-resource: patterns: - - /publishers/{publisher_id} + - /publishers/{publisher} plural: publishers singular: publisher info: @@ -112,11 +112,11 @@ paths: "200": schema: $ref: '#/components/schemas/publisher' - /publishers/{publisher_id}: + /publishers/{publisher}: delete: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string @@ -126,7 +126,7 @@ paths: get: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string @@ -137,7 +137,7 @@ paths: patch: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string @@ -159,11 +159,11 @@ paths: "200": schema: $ref: '#/components/schemas/publisher' - /publishers/{publisher_id}/books: + /publishers/{publisher}/books: get: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string @@ -185,7 +185,7 @@ paths: post: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string @@ -202,16 +202,16 @@ paths: "200": schema: $ref: '#/components/schemas/book' - /publishers/{publisher_id}/books/{book_id}: + /publishers/{publisher}/books/{book}: delete: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string - in: path - name: book_id + name: book required: true schema: {} type: string @@ -221,12 +221,12 @@ paths: get: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string - in: path - name: book_id + name: book required: true schema: {} type: string @@ -237,12 +237,12 @@ paths: patch: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string - in: path - name: book_id + name: book required: true schema: {} type: string @@ -257,7 +257,7 @@ paths: put: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string @@ -269,16 +269,16 @@ paths: "200": schema: $ref: '#/components/schemas/book' - /publishers/{publisher_id}/books/{book_id}/editions: + /publishers/{publisher}/books/{book}/editions: get: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string - in: path - name: book_id + name: book required: true schema: {} type: string @@ -300,12 +300,12 @@ paths: post: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string - in: path - name: book_id + name: book required: true schema: {} type: string @@ -322,21 +322,21 @@ paths: "200": schema: $ref: '#/components/schemas/book-edition' - /publishers/{publisher_id}/books/{book_id}/editions/{book_edition_id}: + /publishers/{publisher}/books/{book}/editions/{book-edition}: delete: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string - in: path - name: book_id + name: book required: true schema: {} type: string - in: path - name: book_edition_id + name: book-edition required: true schema: {} type: string @@ -346,17 +346,17 @@ paths: get: parameters: - in: path - name: publisher_id + name: publisher required: true schema: {} type: string - in: path - name: book_id + name: book required: true schema: {} type: string - in: path - name: book_edition_id + name: book-edition required: true schema: {} type: string diff --git a/writer/openapi/openapi.go b/writer/openapi/openapi.go index b9e17f1..fff5961 100644 --- a/writer/openapi/openapi.go +++ b/writer/openapi/openapi.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/aep-dev/aepc/constants" - "github.com/aep-dev/aepc/internal/utils" "github.com/aep-dev/aepc/parser" "github.com/aep-dev/aepc/schema" "github.com/aep-dev/aepc/writer/writer_utils" @@ -58,7 +57,7 @@ func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) { } patterns := []string{} schemaRef := fmt.Sprintf("#/components/schemas/%v", r.Kind) - singular := utils.KebabToSnakeCase(r.Kind) + singular := r.Kind collection := writer_utils.CollectionName(r) // declare some commonly used objects, to be used later. bodyParam := ParameterInfo{ @@ -70,7 +69,7 @@ func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) { } idParam := ParameterInfo{ In: "path", - Name: fmt.Sprintf("%s_id", singular), + Name: singular, Required: true, Type: "string", } @@ -80,7 +79,7 @@ func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) { }, } for _, pwp := range *parentPWPS { - resourcePath := fmt.Sprintf("%s/%s/{%s_id}", pwp.Pattern, collection, singular) + resourcePath := fmt.Sprintf("%s/%s/{%s}", pwp.Pattern, collection, singular) patterns = append(patterns, resourcePath) if r.Methods.List != nil { listPath := fmt.Sprintf("%s/%s", pwp.Pattern, collection) @@ -269,10 +268,11 @@ func generateParentPatternsWithParams(r *parser.ParsedResource) *[]PathWithParam } pwps := []PathWithParams{} for _, parent := range r.ParsedParents { - basePattern := fmt.Sprintf("/%s/{%s_id}", writer_utils.CollectionName(parent), lowerizer.String(parent.Kind)) + singular := parent.Kind + basePattern := fmt.Sprintf("/%s/{%s}", writer_utils.CollectionName(parent), singular) baseParam := ParameterInfo{ In: "path", - Name: fmt.Sprintf("%s_id", lowerizer.String(parent.Kind)), + Name: singular, Required: true, Type: "string", }