Skip to content

Commit 2c57d8e

Browse files
authored
Add helper to setup operation (#7)
1 parent 26850d0 commit 2c57d8e

File tree

8 files changed

+54
-168
lines changed

8 files changed

+54
-168
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
steps:
1414
- uses: actions/checkout@v2
1515
- name: golangci-lint
16-
uses: golangci/golangci-lint-action@v0.1.7
16+
uses: golangci/golangci-lint-action@v0.2.0
1717
with:
1818
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
1919
version: v1.26

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
GOLANGCI_LINT_VERSION := "v1.27.0"
2+
13
gen:
24
cd resources/schema/ && json-cli gen-go openapi3.json --output ../../openapi3/entities.go --package-name openapi3 --with-zero-values --fluent-setters --root-name Spec
35
gofmt -w ./openapi3/entities.go
46

57
lint:
6-
golangci-lint run ./...
8+
@test -s $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /tmp $(GOLANGCI_LINT_VERSION) && mv /tmp/golangci-lint $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION))
9+
@$(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) run ./...

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ go 1.13
55
require (
66
github.com/stretchr/testify v1.5.1
77
github.com/swaggest/assertjson v1.0.0
8-
github.com/swaggest/jsonschema-go v0.3.0
9-
github.com/swaggest/refl v0.1.2
10-
github.com/swaggest/swgen v0.6.22
11-
gopkg.in/yaml.v2 v2.2.8
8+
github.com/swaggest/jsonschema-go v0.3.2
9+
github.com/swaggest/refl v0.1.3
10+
github.com/swaggest/swgen v0.6.23
11+
gopkg.in/yaml.v2 v2.3.0
1212
)

go.sum

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ github.com/swaggest/assertjson v1.0.0/go.mod h1:mE5ltBbrB+Ya8Xar5OMITxya76vwLZMo
5454
github.com/swaggest/jsonschema-go v0.2.1 h1:SVg+zXQ46w6ewR7jZDRVbpKKC+V7Y6Xn/teC2pRS2bM=
5555
github.com/swaggest/jsonschema-go v0.2.1/go.mod h1:QFauBdPTrU1UltwocM5FzOWnVjVVtcWkJWG3NlK9sV0=
5656
github.com/swaggest/jsonschema-go v0.2.4/go.mod h1:m4VV88Gbi7lCrt9ckJzigK1rMlEeFjdZUkJr1o5MnDE=
57-
github.com/swaggest/jsonschema-go v0.3.0 h1:QysAjTu2u05WMpkMkKzVR8QLMXYGsx6r9BCKInJ99Hk=
58-
github.com/swaggest/jsonschema-go v0.3.0/go.mod h1:dZ+BzW9hUiHkb/jm+gDJnS8TPqEd0BrNkUIgJrCL3t0=
57+
github.com/swaggest/jsonschema-go v0.3.2 h1:k0s1XsyoXyY5hDY1ZHVEig8ROZTqPnEdKK4IhQtlWSw=
58+
github.com/swaggest/jsonschema-go v0.3.2/go.mod h1:TrWgbug4p2ZgcxnHDz+CvYvEtJ5KckL/XOV4mSR6FGw=
5959
github.com/swaggest/openapi-go v0.1.3/go.mod h1:Zx4ZgJ7XvlFH9wCOHE7u8RAjLfiHAnCHeaD5kUDujVM=
6060
github.com/swaggest/refl v0.1.0 h1:mz3skba8ewcPmxDryM9rETv4fSuAX4GqNxTuDfPyUM0=
6161
github.com/swaggest/refl v0.1.0/go.mod h1:kmYWhxNEvjfRDdMRqpaR/vLULk/SotJs9HFUCIVMK8o=
6262
github.com/swaggest/refl v0.1.2 h1:BfxmzI4FabeaSHijDEObFB1qaDPevcDsRetAYxg30yw=
6363
github.com/swaggest/refl v0.1.2/go.mod h1:kmYWhxNEvjfRDdMRqpaR/vLULk/SotJs9HFUCIVMK8o=
64+
github.com/swaggest/refl v0.1.3 h1:cnzEBcCNhYeLPG8Yy9JQixUkxMDsF0mo0GyzblLWrjE=
65+
github.com/swaggest/refl v0.1.3/go.mod h1:kmYWhxNEvjfRDdMRqpaR/vLULk/SotJs9HFUCIVMK8o=
6466
github.com/swaggest/swgen v0.6.20 h1:mGg20AJQu18lkx5NfqCJ9rRpAZjMc4QlXVOIS3JB1Ck=
6567
github.com/swaggest/swgen v0.6.20/go.mod h1:ipkZNfwztgRfbOWUllLZawfmxXprT8flqNJO9XhoMwM=
66-
github.com/swaggest/swgen v0.6.22 h1:vSlCN58I2wLh8i9vcXB0mOkMy8dkVulkw4Rti6HI/9k=
67-
github.com/swaggest/swgen v0.6.22/go.mod h1:gj2yCLONy3kosKjwRtQeT5O9qqlhUvXAiDnbVwBUNFM=
68+
github.com/swaggest/swgen v0.6.23 h1:9COrDC1o34xJS0NeQ2js7qGfo/Rgs16xWT0pQRvGNyY=
69+
github.com/swaggest/swgen v0.6.23/go.mod h1:gj2yCLONy3kosKjwRtQeT5O9qqlhUvXAiDnbVwBUNFM=
6870
github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=
6971
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
7072
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
@@ -110,3 +112,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
110112
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
111113
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
112114
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
115+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
116+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

openapi3/_testdata/openapi.json

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
"content": {
227227
"application/json": {
228228
"schema": {
229-
"$ref": "#/components/schemas/Openapi3TestWeirdResp"
229+
"$ref": "#/components/schemas/Openapi3TestResp"
230230
}
231231
}
232232
}
@@ -248,7 +248,7 @@
248248
"schema": {
249249
"type": "array",
250250
"items": {
251-
"$ref": "#/components/schemas/Openapi3TestWeirdResp"
251+
"$ref": "#/components/schemas/Openapi3TestResp"
252252
}
253253
}
254254
}
@@ -347,16 +347,14 @@
347347
"type": "object",
348348
"additionalProperties": {}
349349
},
350-
"nullableWhatever": {
351-
"nullable": true
352-
},
350+
"nullableWhatever": {},
353351
"parent": {
354352
"$ref": "#/components/schemas/Openapi3TestResp"
355353
},
356354
"recursiveArray": {
357355
"type": "array",
358356
"items": {
359-
"$ref": "#/components/schemas/Openapi3TestWeirdResp"
357+
"$ref": "#/components/schemas/Openapi3TestResp"
360358
}
361359
},
362360
"recursiveStructArray": {
@@ -377,76 +375,6 @@
377375
"type": "string",
378376
"format": "uuid",
379377
"example": "248df4b7-aa70-47b8-a036-33ac447e668d"
380-
},
381-
"Openapi3TestWeirdResp": {
382-
"title": "Sample Response",
383-
"type": "object",
384-
"properties": {
385-
"arrayOfAnything": {
386-
"type": "array",
387-
"items": {}
388-
},
389-
"customType": {
390-
"$ref": "#/components/schemas/Openapi3TestISOWeek"
391-
},
392-
"field1": {
393-
"type": "integer"
394-
},
395-
"field2": {
396-
"type": "string"
397-
},
398-
"info": {
399-
"required": [
400-
"foo"
401-
],
402-
"type": "object",
403-
"properties": {
404-
"bar": {
405-
"type": "number",
406-
"description": "This is Bar."
407-
},
408-
"foo": {
409-
"pattern": "\\d+",
410-
"type": "string"
411-
}
412-
}
413-
},
414-
"map": {
415-
"type": "object",
416-
"additionalProperties": {
417-
"type": "integer"
418-
}
419-
},
420-
"mapOfAnything": {
421-
"type": "object",
422-
"additionalProperties": {}
423-
},
424-
"nullableWhatever": {
425-
"nullable": true
426-
},
427-
"parent": {
428-
"$ref": "#/components/schemas/Openapi3TestResp"
429-
},
430-
"recursiveArray": {
431-
"type": "array",
432-
"items": {
433-
"$ref": "#/components/schemas/Openapi3TestWeirdResp"
434-
}
435-
},
436-
"recursiveStructArray": {
437-
"type": "array",
438-
"items": {
439-
"$ref": "#/components/schemas/Openapi3TestResp"
440-
}
441-
},
442-
"uuid": {
443-
"$ref": "#/components/schemas/Openapi3TestUUID"
444-
},
445-
"whatever": {}
446-
},
447-
"description": "This is a sample response.",
448-
"nullable": true,
449-
"x-foo": "bar"
450378
}
451379
}
452380
}

openapi3/_testdata/resp_schema.json

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@
5353
"additionalProperties": {},
5454
"type": "object"
5555
},
56-
"nullableWhatever": {
57-
"type": "null"
58-
},
56+
"nullableWhatever": {},
5957
"parent": {
6058
"$ref": "#/components/schemas/Openapi3TestResp"
6159
},
6260
"recursiveArray": {
6361
"items": {
64-
"$ref": "#/components/schemas/Openapi3TestWeirdResp"
62+
"$ref": "#/components/schemas/Openapi3TestResp"
6563
},
6664
"type": "array"
6765
},
@@ -85,78 +83,6 @@
8583
],
8684
"type": "string",
8785
"format": "uuid"
88-
},
89-
"Openapi3TestWeirdResp": {
90-
"title": "Sample Response",
91-
"description": "This is a sample response.",
92-
"properties": {
93-
"arrayOfAnything": {
94-
"items": {},
95-
"type": "array"
96-
},
97-
"customType": {
98-
"$ref": "#/components/schemas/Openapi3TestISOWeek"
99-
},
100-
"field1": {
101-
"type": "integer"
102-
},
103-
"field2": {
104-
"type": "string"
105-
},
106-
"info": {
107-
"required": [
108-
"foo"
109-
],
110-
"properties": {
111-
"bar": {
112-
"description": "This is Bar.",
113-
"type": "number"
114-
},
115-
"foo": {
116-
"pattern": "\\d+",
117-
"type": "string"
118-
}
119-
},
120-
"type": "object"
121-
},
122-
"map": {
123-
"additionalProperties": {
124-
"type": "integer"
125-
},
126-
"type": "object"
127-
},
128-
"mapOfAnything": {
129-
"additionalProperties": {},
130-
"type": "object"
131-
},
132-
"nullableWhatever": {
133-
"type": "null"
134-
},
135-
"parent": {
136-
"$ref": "#/components/schemas/Openapi3TestResp"
137-
},
138-
"recursiveArray": {
139-
"items": {
140-
"$ref": "#/components/schemas/Openapi3TestWeirdResp"
141-
},
142-
"type": "array"
143-
},
144-
"recursiveStructArray": {
145-
"items": {
146-
"$ref": "#/components/schemas/Openapi3TestResp"
147-
},
148-
"type": "array"
149-
},
150-
"uuid": {
151-
"$ref": "#/components/schemas/Openapi3TestUUID"
152-
},
153-
"whatever": {}
154-
},
155-
"type": [
156-
"object",
157-
"null"
158-
],
159-
"x-foo": "bar"
16086
}
16187
}
16288
}

openapi3/helper.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func (p *PathItem) WithOperation(method string, operation Operation) *PathItem {
2323

2424
var regexFindPathParameter = regexp.MustCompile(`{([^}:]+)(:[^/]+)?(?:})`)
2525

26-
// AddOperation validates and sets operation by path and method.
27-
func (s *Spec) AddOperation(method, path string, operation Operation) error {
26+
// SetupOperation creates operation if it is not present and applies setup functions.
27+
func (s *Spec) SetupOperation(method, path string, setup ...func(*Operation) error) error {
2828
method = strings.ToLower(method)
2929
pathParametersSubmatches := regexFindPathParameter.FindAllStringSubmatch(path, -1)
3030

@@ -36,10 +36,6 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {
3636
}
3737

3838
pathItem := s.Paths.MapOfPathItemValues[path]
39-
if _, found := pathItem.MapOfOperationValues[method]; found {
40-
return errors.New("operation with method and path already exists")
41-
}
42-
4339
pathParams := map[string]bool{}
4440

4541
if len(pathParametersSubmatches) > 0 {
@@ -54,6 +50,15 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {
5450

5551
var errs []string
5652

53+
operation := pathItem.MapOfOperationValues[method]
54+
55+
for _, f := range setup {
56+
err := f(&operation)
57+
if err != nil {
58+
return err
59+
}
60+
}
61+
5762
paramIndex := make(map[string]bool, len(operation.Parameters))
5863

5964
for _, p := range operation.Parameters {
@@ -90,3 +95,20 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {
9095

9196
return nil
9297
}
98+
99+
// AddOperation validates and sets operation by path and method.
100+
//
101+
// It will fail if operation with method and path already exists.
102+
func (s *Spec) AddOperation(method, path string, operation Operation) error {
103+
method = strings.ToLower(method)
104+
pathItem := s.Paths.MapOfPathItemValues[path]
105+
106+
if _, found := pathItem.MapOfOperationValues[method]; found {
107+
return errors.New("operation with method and path already exists")
108+
}
109+
110+
return s.SetupOperation(method, path, func(op *Operation) error {
111+
*op = operation
112+
return nil
113+
})
114+
}

openapi3/reflect_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ func TestReflector_SetJSONResponse(t *testing.T) {
217217
Schema.ToJSONSchema(s)
218218
jsb, err = json.MarshalIndent(js, "", " ")
219219
require.NoError(t, err)
220+
221+
require.NoError(t, ioutil.WriteFile("_testdata/resp_schema_last_run.json", jsb, 0600))
222+
220223
expected, err = ioutil.ReadFile("_testdata/resp_schema.json")
221224
require.NoError(t, err)
222225
assertjson.Equal(t, expected, jsb, string(jsb))

0 commit comments

Comments
 (0)