Skip to content

Commit 59b018c

Browse files
authored
Some coding style fixes and cleaning up (#1093)
* openapi3filter: some coding style fixes Signed-off-by: Pierre Fenoll <[email protected]> * aannd more cleaning up Signed-off-by: Pierre Fenoll <[email protected]> --------- Signed-off-by: Pierre Fenoll <[email protected]>
1 parent a33c5f8 commit 59b018c

File tree

9 files changed

+22
-56
lines changed

9 files changed

+22
-56
lines changed

maps.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ value_types+=('*PathItem')
1515
value_types+=('*PathItem')
1616

1717
deref_vs=()
18-
deref_vs+=('*Response = v.Value')
19-
deref_vs+=('*PathItem = v')
20-
deref_vs+=('*PathItem = v')
18+
deref_vs+=('v.Value')
19+
deref_vs+=('v')
20+
deref_vs+=('v')
2121

2222
names=()
2323
names+=('responses')
@@ -145,8 +145,7 @@ func (${name} ${type#'*'}) JSONLookup(token string) (any, error) {
145145
} else if ref := v.Ref; ref != "" {
146146
return &Ref{Ref: ref}, nil
147147
} else {
148-
var vv ${deref_v}
149-
return vv, nil
148+
return ${deref_v}, nil
150149
}
151150
}
152151

openapi2conv/openapi2_conv.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func ToV3WithLoader(doc2 *openapi2.T, loader *openapi3.Loader, location *url.URL
2828

2929
if host := doc2.Host; host != "" {
3030
if strings.Contains(host, "/") {
31-
err := fmt.Errorf("invalid host %q. This MUST be the host only and does not include the scheme nor sub-paths.", host)
31+
err := fmt.Errorf("invalid host %q. This MUST be the host only and does not include the scheme nor sub-paths", host)
3232
return nil, err
3333
}
3434
schemes := doc2.Schemes
@@ -679,12 +679,11 @@ func FromV3(doc3 *openapi3.T) (*openapi2.T, error) {
679679
isHTTP := false
680680
servers := doc3.Servers
681681
for i, server := range servers {
682-
parsedURL, err := url.Parse(server.URL)
683-
if err == nil {
684-
// See which schemes seem to be supported
685-
if parsedURL.Scheme == "https" {
682+
if parsedURL, err := url.Parse(server.URL); err == nil {
683+
switch parsedURL.Scheme {
684+
case "https":
686685
isHTTPS = true
687-
} else if parsedURL.Scheme == "http" {
686+
case "http":
688687
isHTTP = true
689688
}
690689
// The first server is assumed to provide the base path

openapi3/internalize_refs.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,11 @@ func DefaultRefNameResolver(doc *T, ref ComponentRef) string {
7070

7171
// Trim the common prefix with the root doc path.
7272
if doc.url != nil {
73-
commonDir := path.Dir(doc.url.Path)
74-
for {
75-
if commonDir == "." { // no common prefix
76-
break
77-
}
78-
73+
for commonDir := path.Dir(doc.url.Path); /*no common prefix*/ commonDir != "."; commonDir = path.Dir(commonDir) {
7974
if p, found := cutDirectories(filePath, commonDir); found {
8075
filePath = p
8176
break
8277
}
83-
84-
commonDir = path.Dir(commonDir)
8578
}
8679
}
8780
}

openapi3/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,5 +1229,5 @@ func (loader *Loader) resolvePathItemRef(doc *T, pathItem *PathItem, documentPat
12291229
}
12301230

12311231
func unescapeRefString(ref string) string {
1232-
return strings.Replace(strings.Replace(ref, "~1", "/", -1), "~0", "~", -1)
1232+
return strings.ReplaceAll(strings.ReplaceAll(ref, "~1", "/"), "~0", "~")
12331233
}

openapi3/maplike.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ func (responses Responses) JSONLookup(token string) (any, error) {
7171
} else if ref := v.Ref; ref != "" {
7272
return &Ref{Ref: ref}, nil
7373
} else {
74-
var vv *Response = v.Value
75-
return vv, nil
74+
return v.Value, nil
7675
}
7776
}
7877

@@ -213,8 +212,7 @@ func (callback Callback) JSONLookup(token string) (any, error) {
213212
} else if ref := v.Ref; ref != "" {
214213
return &Ref{Ref: ref}, nil
215214
} else {
216-
var vv *PathItem = v
217-
return vv, nil
215+
return v, nil
218216
}
219217
}
220218

@@ -355,8 +353,7 @@ func (paths Paths) JSONLookup(token string) (any, error) {
355353
} else if ref := v.Ref; ref != "" {
356354
return &Ref{Ref: ref}, nil
357355
} else {
358-
var vv *PathItem = v
359-
return vv, nil
356+
return v, nil
360357
}
361358
}
362359

openapi3/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func (addProps *AdditionalProperties) UnmarshalJSON(data []byte) error {
246246
addProps.Schema = &SchemaRef{Value: &Schema{}}
247247
} else {
248248
buf := new(bytes.Buffer)
249-
json.NewEncoder(buf).Encode(y)
249+
_ = json.NewEncoder(buf).Encode(y)
250250
if err := json.NewDecoder(buf).Decode(&addProps.Schema); err != nil {
251251
return err
252252
}

openapi3filter/req_resp_decoder.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -899,26 +899,6 @@ func deepSet(m map[string]any, keys []string, value any) {
899899
m[keys[len(keys)-1]] = value
900900
}
901901

902-
func findNestedSchema(parentSchema *openapi3.SchemaRef, keys []string) (*openapi3.SchemaRef, error) {
903-
currentSchema := parentSchema
904-
for _, key := range keys {
905-
if currentSchema.Value.Type.Includes(openapi3.TypeArray) {
906-
currentSchema = currentSchema.Value.Items
907-
} else {
908-
propertySchema, ok := currentSchema.Value.Properties[key]
909-
if !ok {
910-
if currentSchema.Value.AdditionalProperties.Schema == nil {
911-
return nil, fmt.Errorf("nested schema for key %q not found", key)
912-
}
913-
currentSchema = currentSchema.Value.AdditionalProperties.Schema
914-
continue
915-
}
916-
currentSchema = propertySchema
917-
}
918-
}
919-
return currentSchema, nil
920-
}
921-
922902
// makeObject returns an object that contains properties from props.
923903
func makeObject(props map[string]string, schema *openapi3.SchemaRef) (map[string]any, error) {
924904
mobj := make(map[string]any)
@@ -1482,8 +1462,7 @@ func MultipartBodyDecoder(body io.Reader, header http.Header, schema *openapi3.S
14821462
}
14831463

14841464
// Parse primitive types when no content type is explicitely provided, or the content type is set to text/plain
1485-
contentType := partHeader.Get(headerCT)
1486-
if contentType == "" || contentType == "text/plain" {
1465+
if contentType := partHeader.Get(headerCT); contentType == "" || contentType == "text/plain" {
14871466
if value, err = parsePrimitive(value.(string), valueSchema); err != nil {
14881467
if v, ok := err.(*ParseError); ok {
14891468
return nil, &ParseError{path: []any{name}, Cause: v}

openapi3filter/validate_request.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ func validateSecurityRequirement(ctx context.Context, input *RequestValidationIn
423423
defer input.Request.Body.Close()
424424

425425
var err error
426-
data, err = io.ReadAll(input.Request.Body)
427-
if err != nil {
426+
if data, err = io.ReadAll(input.Request.Body); err != nil {
428427
return &RequestError{
429428
Input: input,
430429
Reason: "reading failed",

routers/gorillamux/router.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ func newSrv(serverURL string, server *openapi3.Server, varsUpdater varsf) (srv,
207207
var blURL, brURL = strings.Repeat("-", 50), strings.Repeat("_", 50)
208208

209209
func bEncode(s string) string {
210-
s = strings.Replace(s, "{", blURL, -1)
211-
s = strings.Replace(s, "}", brURL, -1)
210+
s = strings.ReplaceAll(s, "{", blURL)
211+
s = strings.ReplaceAll(s, "}", brURL)
212212
return s
213213
}
214214
func bDecode(s string) string {
215-
s = strings.Replace(s, blURL, "{", -1)
216-
s = strings.Replace(s, brURL, "}", -1)
215+
s = strings.ReplaceAll(s, blURL, "{")
216+
s = strings.ReplaceAll(s, brURL, "}")
217217
return s
218218
}
219219

@@ -250,7 +250,7 @@ func permutePart(part0 string, srv *openapi3.Server) []string {
250250
for i := 0; i < max; i++ {
251251
part := part0
252252
for name, mas := range var2val {
253-
part = strings.Replace(part, name, mas.s[i%len(mas.s)], -1)
253+
part = strings.ReplaceAll(part, name, mas.s[i%len(mas.s)])
254254
}
255255
partsMap[part] = struct{}{}
256256
}

0 commit comments

Comments
 (0)