Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discard unknown fields when parsing Feed configs #123

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/api/admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ Message describing additional options for the GTFS realtime feeds.
| nyct_trips_options | [GtfsRealtimeOptions.NyctTripsOptions](admin.md#GtfsRealtimeOptions.NyctTripsOptions) |
| nyct_alerts_options | [GtfsRealtimeOptions.NyctAlertsOptions](admin.md#GtfsRealtimeOptions.NyctAlertsOptions) |
| reassign_stop_sequences | bool | If true, stop sequences in the GTFS realtime feed data are ignored, and alternative stop sequences are generated and assigned by Transiter. This setting is designed for buggy GTFS realtime feeds in which stop sequences (incorrectly) change between updates. In many cases Transiter is able to generate stop sequences that are correct and stable across updates.<br /><br />This should not be used for systems where a trip can call at the same stop multiple times.
| only_process_full_entities | bool | If true, only process entities in a feed if the message contains the full entity. This is useful for cases where there are multiple feeds for the same system, and some feeds contain only partial information about entities.
| only_process_full_entities | bool | Deprecated: this field is ignored.



Expand Down
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