Skip to content

Commit 1e1411d

Browse files
authored
Merge pull request #59 from parvez3019/feature/operation-id-field
2 parents b56b45f + a5bc5c7 commit 1e1411d

File tree

7 files changed

+111
-13
lines changed

7 files changed

+111
-13
lines changed

integration_test/test_data/handler/restaurants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// @Param filter query model.Filter false "In json format"
1414
// @Param extra.field query string false "extra field"
1515
// @Success 200 {object} model.GetRestaurantsResponse
16+
// @OperationId GetRestaurants
1617
// @Router /restaurants [get]
1718
func GetRestaurants() {
1819
}
@@ -29,6 +30,7 @@ func GetRestaurants() {
2930
// @failure 400 {object} aliasValidationError
3031
// @failure 404 {object} ErrResponse
3132
// @failure 500 {object} ErrResponse
33+
// @OperationId GetPogs
3234
// @route assortment/planogram [get]
3335
func GetPogs() {}
3436

integration_test/test_data/handler/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package handler
55
// @Header model.Headers
66
// @Param request body model.CreateUserRequest true "Create User Request"
77
// @Success 200 {object} model.CreateUserResponse
8+
// @OperationId CreateUser
89
// @Router /user [post]
910
func CreateUser() {
1011
}
11-

integration_test/test_data/oas.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ openapi: 3.0.0
157157
paths:
158158
/restaurants:
159159
get:
160+
operationId: GetRestaurants
160161
description: ' Returns a list of restaurants based on filter request'
161162
parameters:
162163
- $ref: '#/components/parameters/Client-Version'
@@ -197,6 +198,7 @@ paths:
197198
summary: Get restaurants list
198199
/user:
199200
post:
201+
operationId: CreateUser
200202
description: ' Creates & Returns an User based on the request'
201203
parameters:
202204
- $ref: '#/components/parameters/Client-Version'
@@ -216,6 +218,72 @@ paths:
216218
$ref: '#/components/schemas/CreateUserResponse'
217219
description: ""
218220
summary: Create User
221+
/assortment/planogram:
222+
get:
223+
operationId: GetPogs
224+
description: Returns planogram based on query params
225+
parameters:
226+
- name: id
227+
in: query
228+
required: true
229+
description: Use as filter.id! Planogram dbKey [comma separated list]
230+
schema:
231+
type: string
232+
- name: locationId
233+
in: query
234+
required: true
235+
description: Use as filter.locationId! Location ID
236+
schema:
237+
type: string
238+
- name: include
239+
in: query
240+
required: false
241+
description: Includes. Can be: position, fixture, liveFlrFixture
242+
schema:
243+
type: string
244+
- name: commodity
245+
in: query
246+
required: false
247+
description: Use as filter.commodity! Commodity
248+
schema:
249+
type: string
250+
- name: commodityGroup
251+
in: query
252+
required: false
253+
description: Use as filter.commodityGroup! Commodity Group
254+
schema:
255+
type: string
256+
- name: isDigitalScreen
257+
in: query
258+
required: false
259+
description: Use as filter.isDigitalScreen! IsDigitalScreen. Can be: true, false
260+
schema:
261+
type: string
262+
responses:
263+
"200":
264+
description: ""
265+
content:
266+
application/json:
267+
schema:
268+
$ref: '#/components/schemas/GetPogsResponse'
269+
"400":
270+
description: ""
271+
content:
272+
application/json:
273+
schema:
274+
$ref: '#/components/schemas/aliasValidationError'
275+
"404":
276+
description: ""
277+
content:
278+
application/json:
279+
schema:
280+
$ref: '#/components/schemas/ErrResponse'
281+
"500":
282+
description: ""
283+
content:
284+
application/json:
285+
schema:
286+
$ref: '#/components/schemas/ErrResponse'
219287
security:
220288
- AuthorizationHeader:
221289
- read

integration_test/test_data/spec/expected.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"summary": "Get restaurants list",
5151
"description": " Returns a list of restaurants based on filter request",
52+
"operationId": "GetRestaurants",
5253
"parameters": [
5354
{
5455
"$ref": "#/components/parameters/Client-Version"
@@ -141,6 +142,7 @@
141142
},
142143
"summary": "Create User",
143144
"description": " Creates \u0026 Returns an User based on the request",
145+
"operationId": "CreateUser",
144146
"parameters": [
145147
{
146148
"$ref": "#/components/parameters/Client-Version"
@@ -210,6 +212,7 @@
210212
},
211213
"summary": "Get Planograms.",
212214
"description": " Returns planogram based on query params.",
215+
"operationId": "GetPogs",
213216
"parameters": [
214217
{
215218
"name": "id",

integration_test/test_data/spec/expected_with_pkg.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"summary": "Get restaurants list",
5151
"description": " Returns a list of restaurants based on filter request",
52+
"operationId": "GetRestaurants",
5253
"parameters": [
5354
{
5455
"$ref": "#/components/parameters/Client-Version"
@@ -141,6 +142,7 @@
141142
},
142143
"summary": "Create User",
143144
"description": " Creates \u0026 Returns an User based on the request",
145+
"operationId": "CreateUser",
144146
"parameters": [
145147
{
146148
"$ref": "#/components/parameters/Client-Version"
@@ -210,6 +212,7 @@
210212
},
211213
"summary": "Get Planograms.",
212214
"description": " Returns planogram based on query params.",
215+
"operationId": "GetPogs",
213216
"parameters": [
214217
{
215218
"name": "id",

openApi3Schema/oas.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ type OperationObject struct {
7575
Tags []string `json:"tags,omitempty"`
7676
Summary string `json:"summary,omitempty"`
7777
Description string `json:"description,omitempty"`
78+
OperationID string `json:"operationId,omitempty"`
7879
Parameters []ParameterObject `json:"parameters,omitempty"`
7980
RequestBody *RequestBodyObject `json:"requestBody,omitempty"`
8081

81-
// Tags
8282
// ExternalDocs
83-
// OperationID
8483
// Callbacks
8584
// Deprecated
8685
// Security

parser/operations/parser.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
package operations
22

33
import (
4-
. "github.com/parvez3019/go-swagger3/openApi3Schema"
5-
"github.com/parvez3019/go-swagger3/parser/model"
6-
"github.com/parvez3019/go-swagger3/parser/schema"
4+
"fmt"
75
"go/ast"
86
"strings"
7+
8+
"github.com/parvez3019/go-swagger3/openApi3Schema"
9+
"github.com/parvez3019/go-swagger3/parser/model"
10+
"github.com/parvez3019/go-swagger3/parser/schema"
911
)
1012

1113
type Parser interface {
1214
Parse(pkgPath, pkgName string, astComments []*ast.Comment) error
1315
}
1416

1517
type parser struct {
16-
OpenAPI *OpenAPIObject
18+
OpenAPI *openApi3Schema.OpenAPIObject
1719

1820
model.Utils
1921
schema.Parser
22+
usedOperationIds map[string]struct{} // Track used operation IDs
2023
}
2124

22-
func NewParser(utils model.Utils, api *OpenAPIObject, schemaParser schema.Parser) Parser {
25+
func NewParser(utils model.Utils, api *openApi3Schema.OpenAPIObject, schemaParser schema.Parser) Parser {
2326
return &parser{
24-
Utils: utils,
25-
OpenAPI: api,
26-
Parser: schemaParser,
27+
Utils: utils,
28+
OpenAPI: api,
29+
Parser: schemaParser,
30+
usedOperationIds: make(map[string]struct{}),
2731
}
2832
}
2933

3034
func (p *parser) Parse(pkgPath, pkgName string, astComments []*ast.Comment) error {
31-
operation := &OperationObject{Responses: map[string]*ResponseObject{}}
35+
operation := &openApi3Schema.OperationObject{Responses: map[string]*openApi3Schema.ResponseObject{}}
3236
if !strings.HasPrefix(pkgPath, p.ModulePath) || (p.HandlerPath != "" && !strings.HasPrefix(pkgPath, p.HandlerPath)) {
3337
return nil
3438
}
@@ -45,7 +49,20 @@ func (p *parser) Parse(pkgPath, pkgName string, astComments []*ast.Comment) erro
4549
return nil
4650
}
4751

48-
func (p *parser) parseOperationFromComment(pkgPath string, pkgName string, comment string, operation *OperationObject) error {
52+
// validateOperationID checks if an operation ID is unique and registers it if it is.
53+
// Returns an error if the operation ID is already used.
54+
func (p *parser) validateOperationID(operationID string) error {
55+
if operationID == "" {
56+
return nil
57+
}
58+
if _, exists := p.usedOperationIds[operationID]; exists {
59+
return fmt.Errorf("operation ID '%s' is not unique", operationID)
60+
}
61+
p.usedOperationIds[operationID] = struct{}{}
62+
return nil
63+
}
64+
65+
func (p *parser) parseOperationFromComment(pkgPath string, pkgName string, comment string, operation *openApi3Schema.OperationObject) error {
4966
attribute := strings.Fields(comment)[0]
5067
switch strings.ToLower(attribute) {
5168
case "@title":
@@ -62,6 +79,12 @@ func (p *parser) parseOperationFromComment(pkgPath string, pkgName string, comme
6279
p.parseResourceAndTag(comment, attribute, operation)
6380
case "@route", "@router":
6481
return p.parseRouteComment(operation, comment)
82+
case "@operationid":
83+
operationID := strings.TrimSpace(comment[len(attribute):])
84+
if err := p.validateOperationID(operationID); err != nil {
85+
return err
86+
}
87+
operation.OperationID = operationID
6588
}
6689
return nil
6790
}

0 commit comments

Comments
 (0)