Skip to content

fix(entoas): GetConfig() has incorrect nil checks against configuration #576

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 2 deletions entoas/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (ex *Extension) generate(next gen.Generator) gen.Generator {
_, err = ex.out.Write(b)
return err
}
return os.WriteFile(filepath.Join(g.Target, "openapi.json"), b, 0644)
return os.WriteFile(filepath.Join(g.Target, "openapi.json"), b, 0o644)
})
}

Expand All @@ -234,7 +234,7 @@ func (c *Config) Decode(o interface{}) error {
// GetConfig loads the entoas.Config from the given *gen.Config object.
func GetConfig(cfg *gen.Config) (*Config, error) {
c := &Config{}
if cfg == nil && cfg.Annotations == nil && cfg.Annotations[c.Name()] == nil {
if cfg != nil && cfg.Annotations != nil && cfg.Annotations[c.Name()] == nil {
return nil, errors.New("entoas extension configuration not found")
}
return c, c.Decode(cfg.Annotations[c.Name()])
Expand Down
95 changes: 47 additions & 48 deletions entoas/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,32 +610,32 @@ func NodeOperations(n *gen.Type) ([]Operation, error) {
return []Operation{OpCreate, OpRead, OpUpdate, OpDelete, OpList}, nil
}
return nil, nil
} else {
// An operation gets exposed if it is either
// - annotated with PolicyExpose or
// - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose.
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
var ops []Operation
for op, opn := range map[Operation]OperationConfig{
OpCreate: ant.Create,
OpRead: ant.Read,
OpUpdate: ant.Update,
OpDelete: ant.Delete,
OpList: ant.List,
} {
// If the operation is explicitly annotated to be exposed do so.
if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) {
ops = append(ops, op)
continue
}
}

// An operation gets exposed if it is either
// - annotated with PolicyExpose or
// - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose.
if err := ant.Decode(n.Annotations[ant.Name()]); err != nil {
return nil, err
}
var ops []Operation
for op, opn := range map[Operation]OperationConfig{
OpCreate: ant.Create,
OpRead: ant.Read,
OpUpdate: ant.Update,
OpDelete: ant.Delete,
OpList: ant.List,
} {
// If the operation is explicitly annotated to be exposed do so.
if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) {
ops = append(ops, op)
continue
}
sort.Slice(ops, func(i, j int) bool {
return ops[i] < ops[j]
})
return ops, nil
}
sort.Slice(ops, func(i, j int) bool {
return ops[i] < ops[j]
})
return ops, nil
}

// EdgeOperations returns the list of operations to expose for this edge.
Expand All @@ -650,36 +650,35 @@ func EdgeOperations(e *gen.Edge) ([]Operation, error) {
if c.DefaultPolicy == PolicyExpose {
if e.Unique {
return []Operation{OpRead}, nil
} else {
return []Operation{OpList}, nil
}
return []Operation{OpList}, nil
}
return nil, nil
}

// An edge-operation gets exposed if it is either
// - annotated with PolicyExpose or
// - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose.
if err := ant.Decode(e.Annotations[ant.Name()]); err != nil {
return nil, err
}
var ops []Operation
m := make(map[Operation]OperationConfig)
if e.Unique {
m[OpRead] = ant.Read
} else {
// An edge-operation gets exposed if it is either
// - annotated with PolicyExpose or
// - not annotated with PolicyExclude and the DefaultPolicy is PolicyExpose.
if err := ant.Decode(e.Annotations[ant.Name()]); err != nil {
return nil, err
}
var ops []Operation
m := make(map[Operation]OperationConfig)
if e.Unique {
m[OpRead] = ant.Read
} else {
m[OpList] = ant.List
}
for op, opn := range m {
if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) {
ops = append(ops, op)
continue
}
m[OpList] = ant.List
}
for op, opn := range m {
if opn.Policy == PolicyExpose || (opn.Policy == PolicyNone && c.DefaultPolicy == PolicyExpose) {
ops = append(ops, op)
continue
}
sort.Slice(ops, func(i, j int) bool {
return ops[i] < ops[j]
})
return ops, nil
}
sort.Slice(ops, func(i, j int) bool {
return ops[i] < ops[j]
})
return ops, nil
}

// reqBody returns the request body for the given node and operation.
Expand Down