Skip to content

Expand or wrap refs to preserve descriptions #1

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

Merged
merged 1 commit into from
Jan 28, 2022
Merged
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
24 changes: 19 additions & 5 deletions codescan/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,12 @@ func (s *schemaBuilder) buildFromType(tpe types.Type, tgt swaggerTypable) error
tgt.Typed("string", sfnm)
return nil
}
if err := s.makeRef(decl, tgt); err != nil {
return err
}
return nil
// Expand substructs, rather than add reference.
// Expanding allows the description to be captured,
// but more importantly allows us to tailor the
// specifiable fields per API when the same object
// is used by multiple APIs.
return s.buildFromStruct(decl, utitpe, tgt.Schema(), make(map[string]string))
}
case *types.Interface:
if decl, ok := s.ctx.FindModel(tio.Pkg().Path(), tio.Name()); ok {
Expand Down Expand Up @@ -350,7 +352,7 @@ func (s *schemaBuilder) buildFromType(tpe types.Type, tgt swaggerTypable) error
}
}
if decl, ok := s.ctx.FindModel(tio.Pkg().Path(), tio.Name()); ok {
if err := s.makeRef(decl, tgt); err != nil {
if err := s.makeEnclosedRef(decl, tgt); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -893,6 +895,18 @@ func (s *schemaBuilder) makeRef(decl *entityDecl, prop swaggerTypable) error {
return nil
}

func (s *schemaBuilder) makeEnclosedRef(decl *entityDecl, prop swaggerTypable) error {
// enclose ref with "allOf" tp preserve instance description
if prop.Schema().AllOf == nil || len(prop.Schema().AllOf) == 0 {
var newSch spec.Schema
if err := s.makeRef(decl, schemaTypable{&newSch, 0}); err != nil {
return err
}
prop.Schema().AllOf = append(prop.Schema().AllOf, newSch)
}
return nil
}

func (s *schemaBuilder) createParser(nm string, schema, ps *spec.Schema, fld *ast.Field) *sectionedParser {
sp := new(sectionedParser)

Expand Down