Skip to content
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion README/tail.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions components.go
Original file line number Diff line number Diff line change
@@ -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)
}
66 changes: 66 additions & 0 deletions components_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading