Skip to content

Commit

Permalink
Discard unknown fields when parsing Feed configs
Browse files Browse the repository at this point in the history
  • Loading branch information
cedarbaum committed Aug 22, 2023
1 parent 9db9070 commit 196758d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ghodss/yaml"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jamespfennell/transiter/internal/convert"
"github.com/jamespfennell/transiter/internal/db/constants"
"github.com/jamespfennell/transiter/internal/gen/api"
"github.com/jamespfennell/transiter/internal/gen/db"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (s *Service) GetSystemConfig(ctx context.Context, req *api.GetSystemConfigR
for _, feed := range feeds {
feed := feed
var feedConfig api.FeedConfig
if err := protojson.Unmarshal([]byte(feed.Config), &feedConfig); err != nil {
if err := convert.UnmarshalJSONAndDiscardUnknown([]byte(feed.Config), &feedConfig); err != nil {
return err
}
reply.Feeds = append(reply.Feeds, &feedConfig)
Expand Down
12 changes: 12 additions & 0 deletions internal/convert/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package convert

import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

func UnmarshalJSONAndDiscardUnknown(b []byte, m proto.Message) error {
return protojson.UnmarshalOptions{
DiscardUnknown: true,
}.Unmarshal(b, m)
}
8 changes: 8 additions & 0 deletions internal/update/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jamespfennell/transiter/internal/gen/api"
"github.com/jamespfennell/transiter/internal/gen/db"
"golang.org/x/exp/slog"
"google.golang.org/protobuf/proto"
)

type UpdateContext struct {
Expand Down Expand Up @@ -46,3 +47,10 @@ func MapKeys[T comparable, V any](in map[T]V) []T {
}
return out
}

func UnmarshallAndDiscardUnknown(b []byte, m proto.Message) error {
unmarshalOptions := proto.UnmarshalOptions{
DiscardUnknown: true,
}
return unmarshalOptions.Unmarshal(b, m)
}
4 changes: 2 additions & 2 deletions internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jamespfennell/gtfs"
"github.com/jamespfennell/transiter/internal/convert"
"github.com/jamespfennell/transiter/internal/gen/api"
"github.com/jamespfennell/transiter/internal/gen/db"
"github.com/jamespfennell/transiter/internal/monitoring"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/jamespfennell/transiter/internal/update/realtime"
"github.com/jamespfennell/transiter/internal/update/static"
"golang.org/x/exp/slog"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -119,7 +119,7 @@ func markSuccess(feedUpdate *api.FeedUpdate, status api.FeedUpdate_Status) (*api
func run(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, systemID string, feed db.Feed, force bool) (*api.FeedUpdate, error) {
feedUpdate := &api.FeedUpdate{}
var feedConfig api.FeedConfig
if err := protojson.Unmarshal([]byte(feed.Config), &feedConfig); err != nil {
if err := convert.UnmarshalJSONAndDiscardUnknown([]byte(feed.Config), &feedConfig); err != nil {
return markFailure(feedUpdate, api.FeedUpdate_FAILED_INVALID_FEED_CONFIG, fmt.Errorf("failed to parse feed config: %w", err))
}
NormalizeFeedConfig(&feedConfig)
Expand Down

0 comments on commit 196758d

Please sign in to comment.