Skip to content

Commit

Permalink
Merge pull request #27 from mr-pmillz/main
Browse files Browse the repository at this point in the history
Resolved nil pointer dereference
  • Loading branch information
twest-bf authored Sep 20, 2024
2 parents cc99aef + c84e679 commit 31e2862
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 99 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $ sj convert -u https://petstore.swagger.io/v2/swagger.json -o openapi.json`,
log.Error("Command not specified. See the --help flag for usage.")
}
},
Version: "1.8.3",
Version: "1.8.4",
}

func Execute() {
Expand Down
198 changes: 100 additions & 98 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,114 +339,116 @@ func (s SwaggerRequest) AddParametersToRequest(op *openapi3.Operation) SwaggerRe
}

if op.RequestBody != nil {
if op.RequestBody.Value.Content != nil {
for i := range op.RequestBody.Value.Content {
if contentType == "" {
EnforceSingleContentType(i)
} else {
EnforceSingleContentType(contentType)
}
if op.RequestBody.Value.Content.Get(i).Schema != nil {
if op.RequestBody.Value.Content.Get(i).Schema.Value == nil {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Ref, op.RequestBody, 0)
if strings.Contains(i, "json") {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "x-www-form-urlencoded") {
var formData []string
for j := range s.Body {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "xml") {
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}

type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}

var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}

root := Root{
Elements: elements,
}

xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
}
s.BodyData = xmlData
} else {
log.Warnf("Content type not supported. Test this path manually: %s (Content type: %s)\n", s.URL.Path, i)
}
if op.RequestBody.Value != nil {
if op.RequestBody.Value.Content != nil {
for i := range op.RequestBody.Value.Content {
if contentType == "" {
EnforceSingleContentType(i)
} else {
var formData []string
EnforceSingleContentType(contentType)
}
if op.RequestBody.Value.Content.Get(i).Schema != nil {
if op.RequestBody.Value.Content.Get(i).Schema.Value == nil {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Ref, op.RequestBody, 0)
if strings.Contains(i, "json") {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "x-www-form-urlencoded") {
var formData []string
for j := range s.Body {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "xml") {
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}

for j := range op.RequestBody.Value.Content.Get(i).Schema.Value.Properties {
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref != "" {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref, op.RequestBody, 0)
} else {
var valueType string = op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value.Type
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value != nil {
if valueType == "string" {
s.Body[j] = "test"
} else if valueType == "boolean" {
s.Body[j] = false
} else if valueType == "integer" || valueType == "number" {
s.Body[j] = 1
} else {
s.Body[j] = "unknown_type_populate_manually"
}
if i == "application/x-www-form-urlencoded" {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}

if i == "application/x-www-form-urlencoded" {
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "json") || i == "*/*" {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "xml") {
//
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}
var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}

type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}
root := Root{
Elements: elements,
}

var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}
xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
}
s.BodyData = xmlData
} else {
log.Warnf("Content type not supported. Test this path manually: %s (Content type: %s)\n", s.URL.Path, i)
}
} else {
var formData []string

root := Root{
Elements: elements,
for j := range op.RequestBody.Value.Content.Get(i).Schema.Value.Properties {
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref != "" {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref, op.RequestBody, 0)
} else {
var valueType string = op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value.Type
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value != nil {
if valueType == "string" {
s.Body[j] = "test"
} else if valueType == "boolean" {
s.Body[j] = false
} else if valueType == "integer" || valueType == "number" {
s.Body[j] = 1
} else {
s.Body[j] = "unknown_type_populate_manually"
}
if i == "application/x-www-form-urlencoded" {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
}

xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
if i == "application/x-www-form-urlencoded" {
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "json") || i == "*/*" {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "xml") {
//
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}

type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}

var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}

root := Root{
Elements: elements,
}

xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
}
s.BodyData = xmlData
} else {
s.Body["test"] = "test"
s.BodyData = []byte("test=test")
}
s.BodyData = xmlData
} else {
s.Body["test"] = "test"
s.BodyData = []byte("test=test")
}
}
}
Expand Down

0 comments on commit 31e2862

Please sign in to comment.