mirror of
https://github.com/rancher/norman.git
synced 2025-09-02 07:44:51 +00:00
write writeOnly fields on create
This commit is contained in:
@@ -95,7 +95,11 @@ func (j *JSONResponseWriter) convert(b *builder.Builder, context *types.APIConte
|
||||
if schema == nil {
|
||||
return nil
|
||||
}
|
||||
data, err := b.Construct(schema, input, builder.List)
|
||||
op := builder.List
|
||||
if context.Method == http.MethodPost {
|
||||
op = builder.ListForCreate
|
||||
}
|
||||
data, err := b.Construct(schema, input, op)
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to construct object on output: %v", err)
|
||||
return nil
|
||||
|
@@ -11,14 +11,19 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
Create = Operation("create")
|
||||
Update = Operation("update")
|
||||
Action = Operation("action")
|
||||
List = Operation("list")
|
||||
Create = Operation("create")
|
||||
Update = Operation("update")
|
||||
Action = Operation("action")
|
||||
List = Operation("list")
|
||||
ListForCreate = Operation("listcreate")
|
||||
)
|
||||
|
||||
type Operation string
|
||||
|
||||
func (o Operation) IsList() bool {
|
||||
return strings.HasPrefix(string(o), "list")
|
||||
}
|
||||
|
||||
type Builder struct {
|
||||
apiContext *types.APIContext
|
||||
Version *types.APIVersion
|
||||
@@ -66,7 +71,7 @@ func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{},
|
||||
}
|
||||
|
||||
if value != nil || wasNull {
|
||||
if op != List {
|
||||
if !op.IsList() {
|
||||
if slice, ok := value.([]interface{}); ok {
|
||||
for _, sliceValue := range slice {
|
||||
if sliceValue == nil {
|
||||
@@ -84,7 +89,7 @@ func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{},
|
||||
}
|
||||
result[fieldName] = value
|
||||
|
||||
if op == List && field.Type == "date" && value != "" {
|
||||
if op.IsList() && field.Type == "date" && value != "" {
|
||||
ts, err := convert.ToTimestamp(value)
|
||||
if err == nil {
|
||||
result[fieldName+"TS"] = ts
|
||||
@@ -93,7 +98,7 @@ func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{},
|
||||
}
|
||||
}
|
||||
|
||||
if op == List {
|
||||
if op.IsList() {
|
||||
if !convert.IsEmpty(input["type"]) {
|
||||
result["type"] = input["type"]
|
||||
}
|
||||
@@ -129,7 +134,7 @@ func (b *Builder) checkDefaultAndRequired(schema *types.Schema, input map[string
|
||||
}
|
||||
}
|
||||
|
||||
if op == List && fieldMatchesOp(field, List) && definition.IsReferenceType(field.Type) && !hasKey {
|
||||
if op.IsList() && fieldMatchesOp(field, List) && definition.IsReferenceType(field.Type) && !hasKey {
|
||||
result[fieldName] = nil
|
||||
}
|
||||
}
|
||||
@@ -349,6 +354,8 @@ func fieldMatchesOp(field types.Field, op Operation) bool {
|
||||
return field.Update
|
||||
case List:
|
||||
return !field.WriteOnly
|
||||
case ListForCreate:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user