diff --git a/README.md b/README.md index 8c9c1fa..1e2fa41 100644 --- a/README.md +++ b/README.md @@ -244,7 +244,9 @@ node being changed. `RenameSchemas`) - ✅ Repointing every reference to a duplicate component onto the one that survives, and removing the duplicate (`RedirectSchema`, `RedirectSchemas`) -- ✅ Moving inline definitions into `components`, replacing each with a reference (`ExtractSchema`) +- ✅ Moving inline definitions into `components`, replacing each with a reference (`ExtractSchema`), and + referring to a component (`SchemaRef`) +- ✅ Renaming every component by a rule, such as dropping a suffix (`RenameSchemasFunc`) - ✅ Counting the references to each component (`CountReferences`), and describing them where they are used (`DescribeReferences`) - ✅ Removing the components an edit left without a reference diff --git a/README/tail.md b/README/tail.md index b667782..4f5905f 100644 --- a/README/tail.md +++ b/README/tail.md @@ -10,7 +10,9 @@ node being changed. `RenameSchemas`) - ✅ Repointing every reference to a duplicate component onto the one that survives, and removing the duplicate (`RedirectSchema`, `RedirectSchemas`) -- ✅ Moving inline definitions into `components`, replacing each with a reference (`ExtractSchema`) +- ✅ Moving inline definitions into `components`, replacing each with a reference (`ExtractSchema`), and + referring to a component (`SchemaRef`) +- ✅ Renaming every component by a rule, such as dropping a suffix (`RenameSchemasFunc`) - ✅ Counting the references to each component (`CountReferences`), and describing them where they are used (`DescribeReferences`) - ✅ Removing the components an edit left without a reference diff --git a/components.go b/components.go new file mode 100644 index 0000000..1fa6ecf --- /dev/null +++ b/components.go @@ -0,0 +1,24 @@ +package edit + +import "github.com/MarkRosemaker/openapi" + +// SchemaRef is a reference to the component schema name, as a schema to put where the component is wanted. +func SchemaRef(doc *openapi.Document, name string) *openapi.Schema { + return &openapi.Schema{Ref: &openapi.SchemaRef{Identifier: schemaRefPrefix + name, Value: doc.Components.Schemas[name]}} +} + +// RenameSchemasFunc renames each component schema to what rename makes of its name, as [RenameSchemas] does, such as to +// drop a suffix every name carries. A name rename leaves as it is stays. +// +// It fails, changing nothing, for the reasons RenameSchemas does. +func RenameSchemasFunc(doc *openapi.Document, rename func(string) string) error { + to := map[string]string{} + + for name := range doc.Components.Schemas.ByIndex() { + if newName := rename(name); newName != name { + to[name] = newName + } + } + + return RenameSchemas(doc, to) +} diff --git a/components_test.go b/components_test.go new file mode 100644 index 0000000..221c3ee --- /dev/null +++ b/components_test.go @@ -0,0 +1,66 @@ +package edit_test + +import ( + "errors" + "slices" + "strings" + "testing" + + "github.com/MarkRosemaker/openapi" + edit "github.com/MarkRosemaker/openapi-edit" +) + +// parse reads a document of the paths and schemas given, as JSON. +func parse(t *testing.T, paths, schemas string) *openapi.Document { + t.Helper() + + doc, err := openapi.LoadFromData([]byte(`{"openapi":"3.1.0","info":{"title":"t","version":"1"},"paths":` + paths + + `,"components":{"schemas":` + schemas + `}}`)) + if err != nil { + t.Fatal(err) + } + + return doc +} + +func TestSchemaRef(t *testing.T) { + doc := parse(t, `{}`, `{"Pet":{"type":"object"}}`) + + ref := edit.SchemaRef(doc, "Pet") + if ref.Ref.Identifier != "#/components/schemas/Pet" || ref.Ref.Value != doc.Components.Schemas["Pet"] { + t.Errorf("got %+v", ref.Ref) + } +} + +func TestRenameSchemasFunc(t *testing.T) { + doc := parse(t, `{}`, `{ + "PetRequest":{"type":"object","properties":{"owner":{"$ref":"#/components/schemas/OwnerRequest"}}}, + "OwnerRequest":{"type":"object"}, + "Kind":{"type":"string"} + }`) + + if err := edit.RenameSchemasFunc(doc, func(n string) string { return strings.TrimSuffix(n, "Request") }); err != nil { + t.Fatal(err) + } + + if got := slices.Collect(func(yield func(string) bool) { + for n := range doc.Components.Schemas.ByIndex() { + if !yield(n) { + return + } + } + }); !slices.Equal(got, []string{"Pet", "Owner", "Kind"}) { + t.Errorf("got %q", got) + } + + if ref := doc.Components.Schemas["Pet"].Properties["owner"].Ref; ref.Identifier != "#/components/schemas/Owner" { + t.Errorf("reference left at %s", ref.Identifier) + } + + doc = parse(t, `{}`, `{"PetRequest":{"type":"object"},"Pet":{"type":"object"}}`) + + var exists *edit.ErrSchemaExists + if err := edit.RenameSchemasFunc(doc, func(n string) string { return strings.TrimSuffix(n, "Request") }); !errors.As(err, &exists) { + t.Errorf("onto a name taken: got %v", err) + } +}