|
| 1 | +package modify |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + oas3 "github.com/getkin/kin-openapi/openapi3" |
| 8 | + "github.com/grokify/simplego/fmt/fmtutil" |
| 9 | + "github.com/grokify/swaggman/openapi3" |
| 10 | +) |
| 11 | + |
| 12 | +/* |
| 13 | +func CopyOperation(src oas3.Operation) oas3.Operation { |
| 14 | + dst := oas3.Operation{ |
| 15 | + ExtensionProps: src.ExtensionProps, |
| 16 | + Tags: src.Tags, |
| 17 | + Summary: src.Summary, |
| 18 | + Description: src.Description, |
| 19 | + OperationID: src.OperationID, |
| 20 | + Parameters: src.Parameters, |
| 21 | + RequestBody: src.RequestBody, |
| 22 | + Responses: src.Responses, |
| 23 | + Callbacks: src.Callbacks, |
| 24 | + Deprecated: src.Deprecated, |
| 25 | + Security: src.Security, |
| 26 | + Servers: src.Servers, |
| 27 | + ExternalDocs: src.ExternalDocs, |
| 28 | + } |
| 29 | + return dst |
| 30 | +} |
| 31 | +*/ |
| 32 | + |
| 33 | +func SpecSchemasSetDeprecated(spec *oas3.Swagger, newDeprecated bool) { |
| 34 | + for _, schemaRef := range spec.Components.Schemas { |
| 35 | + if len(schemaRef.Ref) == 0 && schemaRef.Value != nil { |
| 36 | + schemaRef.Value.Deprecated = newDeprecated |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func SpecOperationsSetDeprecated(spec *oas3.Swagger, newDeprecated bool) { |
| 42 | + openapi3.VisitOperations( |
| 43 | + spec, |
| 44 | + func(path, method string, op *oas3.Operation) { |
| 45 | + /*if op == nil { |
| 46 | + return |
| 47 | + }*/ |
| 48 | + |
| 49 | + // op = &*op |
| 50 | + |
| 51 | + op.Summary = "SUMSUMSUMARY" |
| 52 | + op.Deprecated = true |
| 53 | + op.Tags = append(op.Tags, "NEW_TAG") |
| 54 | + |
| 55 | + newOp := oas3.Operation{ |
| 56 | + ExtensionProps: op.ExtensionProps, |
| 57 | + Tags: op.Tags, |
| 58 | + Summary: op.Summary, |
| 59 | + Description: op.Description, |
| 60 | + OperationID: op.OperationID, |
| 61 | + Parameters: op.Parameters, |
| 62 | + RequestBody: op.RequestBody, |
| 63 | + Responses: op.Responses, |
| 64 | + Callbacks: op.Callbacks, |
| 65 | + Deprecated: op.Deprecated, |
| 66 | + Security: op.Security, |
| 67 | + Servers: op.Servers, |
| 68 | + ExternalDocs: op.ExternalDocs, |
| 69 | + } |
| 70 | + |
| 71 | + //newOp := oas3.Operation{} |
| 72 | + // reflect.Copy([]oas3.Operation{*op}, []oas3.Operation{newOp}) |
| 73 | + SpecSetOperation(spec, path, method, newOp) |
| 74 | + if 1 == 0 { |
| 75 | + ps := reflect.ValueOf(op) |
| 76 | + // struct |
| 77 | + s := ps.Elem() |
| 78 | + if s.Kind() == reflect.Struct { |
| 79 | + // exported field |
| 80 | + f := s.FieldByName("Deprecated") |
| 81 | + if f.IsValid() { |
| 82 | + // A Value can be changed only if it is |
| 83 | + // addressable and was not obtained by |
| 84 | + // the use of unexported struct fields. |
| 85 | + if f.CanSet() { |
| 86 | + // change value of N |
| 87 | + if f.Kind() == reflect.Bool { |
| 88 | + f.SetBool(false) |
| 89 | + fmt.Println("SETTING_REEFLECT") |
| 90 | + //x := int64(7) |
| 91 | + /*if !f.OverflowInt(x) { |
| 92 | + f.Set(x) |
| 93 | + }*/ |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if 1 == 0 && op.OperationID == "listGlipGroups" { |
| 101 | + op.Deprecated = false |
| 102 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 103 | + |
| 104 | + fmtutil.PrintJSON(op) |
| 105 | + panic("ABC_VISIT") |
| 106 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 107 | + if 1 == 0 { |
| 108 | + panic("YYY") |
| 109 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 110 | + op.Deprecated = false |
| 111 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 112 | + fmtutil.PrintJSON(op) |
| 113 | + fmt.Printf("SSUM [%s]\n", op.Summary) |
| 114 | + op.Summary = op.Summary + " " + "ABC " |
| 115 | + fmt.Printf("SSUM [%s]\n", op.Summary) |
| 116 | + } |
| 117 | + //panic("ZZZ_LISTGLIPGROUPS") |
| 118 | + } |
| 119 | + |
| 120 | + }, |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +/* |
| 125 | +
|
| 126 | +func SpecOperationsRemoveDeprecated(spec *oas3.Swagger) { |
| 127 | + openapi3.VisitOperations( |
| 128 | + spec, |
| 129 | + func(path, method string, op *oas3.Operation) { |
| 130 | + if op == nil { |
| 131 | + return |
| 132 | + } |
| 133 | + op.Deprecated = false |
| 134 | + // SpecSetOperation(spec, path, method, &*op) |
| 135 | + // op = &*op |
| 136 | + if 1 == 1 && op.OperationID == "listGlipGroups" { |
| 137 | + op.Deprecated = false |
| 138 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 139 | +
|
| 140 | + fmtutil.PrintJSON(*op) |
| 141 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 142 | + if 1 == 0 { |
| 143 | + panic("YYY") |
| 144 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 145 | + op.Deprecated = true |
| 146 | + fmt.Printf("DEP [%v]\n", op.Deprecated) |
| 147 | + fmtutil.PrintJSON(op) |
| 148 | + fmt.Printf("SSUM [%s]\n", op.Summary) |
| 149 | + op.Summary = op.Summary + " " + "ABC " |
| 150 | + fmt.Printf("SSUM [%s]\n", op.Summary) |
| 151 | + } |
| 152 | + panic("ZZZ_LISTGLIPGROUPS") |
| 153 | + } |
| 154 | + }, |
| 155 | + ) |
| 156 | +} |
| 157 | +*/ |
0 commit comments