-
-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handling
default
in request body and parameter schema (#544)
* wip setting defaults for #206 Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com> * introduce body encoders Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com> * re-encode only when needed Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com> * set default for parameter and add more test cases Co-authored-by: Pierre Fenoll <pierrefenoll@gmail.com>
- Loading branch information
Showing
8 changed files
with
669 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package openapi3filter | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func encodeBody(body interface{}, mediaType string) ([]byte, error) { | ||
encoder, ok := bodyEncoders[mediaType] | ||
if !ok { | ||
return nil, &ParseError{ | ||
Kind: KindUnsupportedFormat, | ||
Reason: fmt.Sprintf("%s %q", prefixUnsupportedCT, mediaType), | ||
} | ||
} | ||
return encoder(body) | ||
} | ||
|
||
type bodyEncoder func(body interface{}) ([]byte, error) | ||
|
||
var bodyEncoders = map[string]bodyEncoder{ | ||
"application/json": jsonBodyEncoder, | ||
} | ||
|
||
func jsonBodyEncoder(body interface{}) ([]byte, error) { | ||
return json.Marshal(body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.