Skip to content

Commit e9e7de4

Browse files
committed
Fix doubled slash encoding in url generation
this also simplifies generation and increases readability
1 parent 988c338 commit e9e7de4

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

jsonapi/marshal.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"reflect"
8+
"strings"
89
)
910

1011
// MarshalIdentifier interface is necessary to give an element
@@ -301,25 +302,18 @@ func getStructRelationships(relationer MarshalLinkedRelations, information Serve
301302
// helper method to generate URL fields for `links`
302303
func getLinksForServerInformation(relationer MarshalLinkedRelations, name string, information ServerInformation) map[string]string {
303304
links := map[string]string{}
304-
// generate links if necessary
305+
305306
if information != serverInformationNil {
306-
prefix := ""
307-
baseURL := information.GetBaseURL()
308-
if baseURL != "" {
309-
prefix = baseURL
310-
}
311-
p := information.GetPrefix()
312-
if p != "" {
313-
prefix += "/" + p
314-
}
307+
prefix := strings.Trim(information.GetBaseURL(), "/")
308+
namespace := strings.Trim(information.GetPrefix(), "/")
309+
structType := getStructType(relationer)
315310

316-
if prefix != "" {
317-
links["self"] = fmt.Sprintf("%s/%s/%s/relationships/%s", prefix, getStructType(relationer), relationer.GetID(), name)
318-
links["related"] = fmt.Sprintf("%s/%s/%s/%s", prefix, getStructType(relationer), relationer.GetID(), name)
319-
} else {
320-
links["self"] = fmt.Sprintf("/%s/%s/relationships/%s", getStructType(relationer), relationer.GetID(), name)
321-
links["related"] = fmt.Sprintf("/%s/%s/%s", getStructType(relationer), relationer.GetID(), name)
311+
if namespace != "" {
312+
prefix += "/" + namespace
322313
}
314+
315+
links["self"] = fmt.Sprintf("%s/%s/%s/relationships/%s", prefix, structType, relationer.GetID(), name)
316+
links["related"] = fmt.Sprintf("%s/%s/%s/%s", prefix, structType, relationer.GetID(), name)
323317
}
324318

325319
return links

0 commit comments

Comments
 (0)