Skip to content

Commit c0735c0

Browse files
authored
fix(internal/librarian/dart): add Protobuf specification check (#3965)
Add check toModelConfig function to ensure that the specification format is set to "protobuf" for dart. This will prevent unsupported formats like OpenAPI from being processed by the Dart generator. --------- Signed-off-by: James Wu <jameslynnwu@users.noreply.github.com>
1 parent b477cb2 commit c0735c0

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

internal/librarian/dart/codec.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package dart
1616

1717
import (
18+
"errors"
19+
"fmt"
1820
"strings"
1921

2022
"github.com/googleapis/librarian/internal/config"
@@ -23,7 +25,13 @@ import (
2325
"github.com/googleapis/librarian/internal/sidekick/source"
2426
)
2527

28+
var errInvalidSpecificationFormat = errors.New("dart generation requires protobuf specification format")
29+
2630
func toModelConfig(library *config.Library, ch *config.API, sources *source.Sources) (*parser.ModelConfig, error) {
31+
if library.SpecificationFormat != "" && library.SpecificationFormat != config.SpecProtobuf {
32+
return nil, fmt.Errorf("%w, got %q", errInvalidSpecificationFormat, library.SpecificationFormat)
33+
}
34+
2735
src := addLibraryRoots(library, sources)
2836

2937
if library.DescriptionOverride != "" {

internal/librarian/dart/codec_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ func TestToModelConfig(t *testing.T) {
405405
},
406406
},
407407
},
408+
{
409+
name: "unsupported specification format",
410+
library: &config.Library{
411+
SpecificationFormat: "openapi",
412+
},
413+
channel: &config.API{
414+
Path: "google/api/apikeys/v2",
415+
},
416+
googleapisDir: googleapisDir,
417+
wantErr: errInvalidSpecificationFormat,
418+
},
408419
} {
409420
t.Run(test.name, func(t *testing.T) {
410421
sources := &source.Sources{
@@ -413,10 +424,14 @@ func TestToModelConfig(t *testing.T) {
413424
got, err := toModelConfig(test.library, test.channel, sources)
414425
if test.wantErr != nil {
415426
if !errors.Is(err, test.wantErr) {
416-
t.Errorf("toModelConfig() error = %v, wantErr %v", err, test.wantErr)
427+
t.Errorf("toModelConfig() error: %v, wantErr: %v", err, test.wantErr)
417428
}
418429
return
419430
}
431+
if err != nil {
432+
t.Errorf("toModelConfig() unexpected error: %v", err)
433+
return
434+
}
420435
if diff := cmp.Diff(test.want, got); diff != "" {
421436
t.Errorf("mismatch (-want +got):\n%s", diff)
422437
}

0 commit comments

Comments
 (0)