Skip to content

Commit

Permalink
api: map query parameters to validation options
Browse files Browse the repository at this point in the history
  • Loading branch information
atonks2 committed Nov 22, 2023
1 parent 71d51ac commit 847269f
Show file tree
Hide file tree
Showing 20 changed files with 360 additions and 83 deletions.
1 change: 1 addition & 0 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Class | Method | HTTP request | Description
- [ServiceMessage](docs/ServiceMessage.md)
- [TypeSubType](docs/TypeSubType.md)
- [UnstructuredAddenda](docs/UnstructuredAddenda.md)
- [ValidateOptions](docs/ValidateOptions.md)
- [WireAddress](docs/WireAddress.md)
- [WireAmount](docs/WireAmount.md)
- [WireFile](docs/WireFile.md)
Expand Down
49 changes: 48 additions & 1 deletion client/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ paths:
- Wire Files
/files/create:
post:
description: Create a new File object from either the plaintext or JSON representation.
description: |
Upload a new Wire file, or create one from JSON. When uploading a file, query parameters can be used to configure the FedWireMessage validation options. For JSON requests, validation options are set in the request body under fedWireMessage.validateOptions.
operationId: createWireFile
parameters:
- description: Optional Request ID allows application developer to trace requests
Expand All @@ -78,6 +79,27 @@ paths:
schema:
type: string
style: simple
- description: Optional flag to skip mandatory IMAD validation
explode: true
in: query
name: skipMandatoryIMAD
required: false
schema:
default: false
example: true
type: boolean
style: form
- description: Optional flag to allow SenderSupplied to be nil, which is generally
the case in incoming files.
explode: true
in: query
name: allowMissingSenderSupplied
required: false
schema:
default: false
example: true
type: boolean
style: form
requestBody:
content:
application/json:
Expand Down Expand Up @@ -506,6 +528,9 @@ components:
receiptTime: "1305"
receiptDate: "0401"
receiptApplicationIdentification: RB11
validateOptions:
allowMissingSenderSupplied: true
skipMandatoryIMAD: true
previousMessageIdentifier:
previousMessageIdentifier: Identifier
adjustment:
Expand Down Expand Up @@ -923,6 +948,9 @@ components:
receiptTime: "1305"
receiptDate: "0401"
receiptApplicationIdentification: RB11
validateOptions:
allowMissingSenderSupplied: true
skipMandatoryIMAD: true
previousMessageIdentifier:
previousMessageIdentifier: Identifier
adjustment:
Expand Down Expand Up @@ -1279,6 +1307,8 @@ components:
$ref: '#/components/schemas/RemittanceFreeText'
serviceMessage:
$ref: '#/components/schemas/ServiceMessage'
validateOptions:
$ref: '#/components/schemas/ValidateOptions'
required:
- amount
- businessFunctionCode
Expand Down Expand Up @@ -3094,6 +3124,23 @@ components:
example: Line Twelve Text
maxLength: 35
type: string
ValidateOptions:
example:
allowMissingSenderSupplied: true
skipMandatoryIMAD: true
nullable: true
properties:
skipMandatoryIMAD:
default: false
description: Skip validation of the InputMessageAccountabilityData (IMAD)
field
example: true
type: boolean
allowMissingSenderSupplied:
default: false
description: Allow FedWireMessage.SenderSupplied to be nil
example: true
type: boolean
Error:
properties:
error:
Expand Down
14 changes: 12 additions & 2 deletions client/api_wire_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,20 @@ func (a *WireFilesApiService) AddFEDWireMessageToFile(ctx _context.Context, file

// CreateWireFileOpts Optional parameters for the method 'CreateWireFile'
type CreateWireFileOpts struct {
XRequestID optional.String
XRequestID optional.String
SkipMandatoryIMAD optional.Bool
AllowMissingSenderSupplied optional.Bool
}

/*
CreateWireFile Create file
Create a new File object from either the plaintext or JSON representation.
Upload a new Wire file, or create one from JSON. When uploading a file, query parameters can be used to configure the FedWireMessage validation options. For JSON requests, validation options are set in the request body under fedWireMessage.validateOptions.
- @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
- @param wireFile Content of the Wire file (in json or raw text)
- @param optional nil or *CreateWireFileOpts - Optional Parameters:
- @param "XRequestID" (optional.String) - Optional Request ID allows application developer to trace requests through the system's logs
- @param "SkipMandatoryIMAD" (optional.Bool) - Optional flag to skip mandatory IMAD validation
- @param "AllowMissingSenderSupplied" (optional.Bool) - Optional flag to allow SenderSupplied to be nil, which is generally the case in incoming files.
@return WireFile
*/
Expand All @@ -139,6 +143,12 @@ func (a *WireFilesApiService) CreateWireFile(ctx _context.Context, wireFile Wire
localVarQueryParams := _neturl.Values{}
localVarFormParams := _neturl.Values{}

if localVarOptionals != nil && localVarOptionals.SkipMandatoryIMAD.IsSet() {
localVarQueryParams.Add("skipMandatoryIMAD", parameterToString(localVarOptionals.SkipMandatoryIMAD.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.AllowMissingSenderSupplied.IsSet() {
localVarQueryParams.Add("allowMissingSenderSupplied", parameterToString(localVarOptionals.AllowMissingSenderSupplied.Value(), ""))
}
// to determine the Content-Type header
localVarHTTPContentTypes := []string{"application/json", "text/plain"}

Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *APIClient) prepareRequest(
}
if len(fileBytes) > 0 && fileName != "" {
w.Boundary()
//_, fileNm := filepath.Split(fileName)
// _, fileNm := filepath.Split(fileName)
part, err := w.CreateFormFile(formFileName, filepath.Base(fileName))
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions client/docs/FedWireMessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Name | Type | Description | Notes
**SecondaryRemittanceDocument** | [**SecondaryRemittanceDocument**](SecondaryRemittanceDocument.md) | | [optional]
**RemittanceFreeText** | [**RemittanceFreeText**](RemittanceFreeText.md) | | [optional]
**ServiceMessage** | [**ServiceMessage**](ServiceMessage.md) | | [optional]
**ValidateOptions** | Pointer to [**ValidateOptions**](ValidateOptions.md) | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
12 changes: 12 additions & 0 deletions client/docs/ValidateOptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ValidateOptions

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**SkipMandatoryIMAD** | **bool** | Skip validation of the InputMessageAccountabilityData (IMAD) field | [optional] [default to false]
**AllowMissingSenderSupplied** | **bool** | Allow FedWireMessage.SenderSupplied to be nil | [optional] [default to false]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


4 changes: 3 additions & 1 deletion client/docs/WireFilesApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ No authorization required
Create file

Create a new File object from either the plaintext or JSON representation.
Upload a new Wire file, or create one from JSON. When uploading a file, query parameters can be used to configure the FedWireMessage validation options. For JSON requests, validation options are set in the request body under fedWireMessage.validateOptions.

### Required Parameters

Expand All @@ -88,6 +88,8 @@ Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------

**xRequestID** | **optional.String**| Optional Request ID allows application developer to trace requests through the system's logs |
**skipMandatoryIMAD** | **optional.Bool**| Optional flag to skip mandatory IMAD validation | [default to false]
**allowMissingSenderSupplied** | **optional.Bool**| Optional flag to allow SenderSupplied to be nil, which is generally the case in incoming files. | [default to false]

### Return type

Expand Down
1 change: 1 addition & 0 deletions client/model_fed_wire_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ type FedWireMessage struct {
SecondaryRemittanceDocument SecondaryRemittanceDocument `json:"secondaryRemittanceDocument,omitempty"`
RemittanceFreeText RemittanceFreeText `json:"remittanceFreeText,omitempty"`
ServiceMessage ServiceMessage `json:"serviceMessage,omitempty"`
ValidateOptions *ValidateOptions `json:"validateOptions,omitempty"`
}
18 changes: 18 additions & 0 deletions client/model_validate_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Wire API
*
* Moov Wire implements an HTTP API for creating, parsing, and validating Fedwire messages.
*
* API version: v1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package openapi

// ValidateOptions struct for ValidateOptions
type ValidateOptions struct {
// Skip validation of the InputMessageAccountabilityData (IMAD) field
SkipMandatoryIMAD bool `json:"skipMandatoryIMAD,omitempty"`
// Allow FedWireMessage.SenderSupplied to be nil
AllowMissingSenderSupplied bool `json:"allowMissingSenderSupplied,omitempty"`
}
60 changes: 48 additions & 12 deletions cmd/server/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"

Expand Down Expand Up @@ -96,35 +97,35 @@ func createFile(logger log.Logger, repo WireFileRepository) http.HandlerFunc {

w = wrapResponseWriter(logger, w, r)

req := wire.NewFile()
req.ID = base.ID()

file := wire.NewFile()
if strings.Contains(r.Header.Get("Content-Type"), "application/json") {
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
if err := json.NewDecoder(r.Body).Decode(file); err != nil {
err = logger.LogErrorf("error reading request body: %v", err).Err()
moovhttp.Problem(w, err)
return
}
if err := req.Validate(); err != nil {

if err := file.Validate(); err != nil {
err = logger.LogErrorf("file validation failed: %v", err).Err()
moovhttp.Problem(w, err)
return
}
} else {
file, err := wire.NewReader(r.Body).Read()
f, err := wire.NewReader(r.Body).ReadWithOpts(validateOptsFromQuery(r.URL.Query()))
if err != nil {
err = logger.LogErrorf("error reading file: %v", err).Err()
moovhttp.Problem(w, err)
return
}
req = &file
file = &f
}
if req.ID == "" {
req.ID = base.ID()

if file.ID == "" {
file.ID = base.ID()
}
logger = logger.Set("fileID", log.String(req.ID))
logger = logger.Set("fileID", log.String(file.ID))

if err := repo.saveFile(req); err != nil {
if err := repo.saveFile(file); err != nil {
err = logger.LogErrorf("problem saving file: %v", err).Err()
moovhttp.Problem(w, err)
return
Expand All @@ -136,7 +137,7 @@ func createFile(logger log.Logger, repo WireFileRepository) http.HandlerFunc {

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(req)
json.NewEncoder(w).Encode(file)
}
}

Expand Down Expand Up @@ -390,3 +391,38 @@ func GetWriter(w io.Writer, r *http.Request) (*wire.Writer, error) {
writer = wire.NewWriter(w, lengthFormatOption, newLineFormatOption)
return writer, nil
}

// validateOptsFromQuery returns a ValidateOpts struct based on the query params.
// If no validation query params were provided, opts will be nil.
func validateOptsFromQuery(query url.Values) (opts *wire.ValidateOpts) {
if len(query) == 0 {
return opts
}

const (
skipMandatoryIMAD = "skipMandatoryIMAD"
allowMissingSenderSupplied = "allowMissingSenderSupplied"
)

validationNames := []string{
skipMandatoryIMAD,
allowMissingSenderSupplied,
}

for _, param := range validationNames {
if set, _ := strconv.ParseBool(query.Get(param)); set {
if opts == nil {
opts = &wire.ValidateOpts{}
}

switch param {
case skipMandatoryIMAD:
opts.SkipMandatoryIMAD = true
case allowMissingSenderSupplied:
opts.AllowMissingSenderSupplied = true
}
}
}

return opts
}
Loading

0 comments on commit 847269f

Please sign in to comment.