mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 02:28:09 +00:00 
			
		
		
		
	Change OpenAPI code generator to extract request objects (#14217)
This commit is contained in:
		 Anton Averchenkov
					Anton Averchenkov
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							ef8ce03e70
						
					
				
				
					commit
					dcb5942bd1
				
			| @@ -32,6 +32,9 @@ func NewOASDocument() *OASDocument { | ||||
| 			}, | ||||
| 		}, | ||||
| 		Paths: make(map[string]*OASPathItem), | ||||
| 		Components: OASComponents{ | ||||
| 			Schemas: make(map[string]*OASSchema), | ||||
| 		}, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -78,9 +81,14 @@ func NewOASDocumentFromMap(input map[string]interface{}) (*OASDocument, error) { | ||||
| } | ||||
|  | ||||
| type OASDocument struct { | ||||
| 	Version string                  `json:"openapi" mapstructure:"openapi"` | ||||
| 	Info    OASInfo                 `json:"info"` | ||||
| 	Paths   map[string]*OASPathItem `json:"paths"` | ||||
| 	Version    string                  `json:"openapi" mapstructure:"openapi"` | ||||
| 	Info       OASInfo                 `json:"info"` | ||||
| 	Paths      map[string]*OASPathItem `json:"paths"` | ||||
| 	Components OASComponents           `json:"components"` | ||||
| } | ||||
|  | ||||
| type OASComponents struct { | ||||
| 	Schemas map[string]*OASSchema `json:"schemas"` | ||||
| } | ||||
|  | ||||
| type OASInfo struct { | ||||
| @@ -148,6 +156,7 @@ type OASMediaTypeObject struct { | ||||
| } | ||||
|  | ||||
| type OASSchema struct { | ||||
| 	Ref         string                `json:"$ref,omitempty"` | ||||
| 	Type        string                `json:"type,omitempty"` | ||||
| 	Description string                `json:"description,omitempty"` | ||||
| 	Properties  map[string]*OASSchema `json:"properties,omitempty"` | ||||
| @@ -204,9 +213,9 @@ var ( | ||||
| ) | ||||
|  | ||||
| // documentPaths parses all paths in a framework.Backend into OpenAPI paths. | ||||
| func documentPaths(backend *Backend, doc *OASDocument) error { | ||||
| func documentPaths(backend *Backend, requestResponsePrefix string, doc *OASDocument) error { | ||||
| 	for _, p := range backend.Paths { | ||||
| 		if err := documentPath(p, backend.SpecialPaths(), backend.BackendType, doc); err != nil { | ||||
| 		if err := documentPath(p, backend.SpecialPaths(), requestResponsePrefix, backend.BackendType, doc); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| @@ -215,7 +224,7 @@ func documentPaths(backend *Backend, doc *OASDocument) error { | ||||
| } | ||||
|  | ||||
| // documentPath parses a framework.Path into one or more OpenAPI paths. | ||||
| func documentPath(p *Path, specialPaths *logical.Paths, backendType logical.BackendType, doc *OASDocument) error { | ||||
| func documentPath(p *Path, specialPaths *logical.Paths, requestResponsePrefix string, backendType logical.BackendType, doc *OASDocument) error { | ||||
| 	var sudoPaths []string | ||||
| 	var unauthPaths []string | ||||
|  | ||||
| @@ -224,7 +233,7 @@ func documentPath(p *Path, specialPaths *logical.Paths, backendType logical.Back | ||||
| 		unauthPaths = specialPaths.Unauthenticated | ||||
| 	} | ||||
|  | ||||
| 	// Convert optional parameters into distinct patterns to be process independently. | ||||
| 	// Convert optional parameters into distinct patterns to be processed independently. | ||||
| 	paths := expandPattern(p.Pattern) | ||||
|  | ||||
| 	for _, path := range paths { | ||||
| @@ -358,10 +367,12 @@ func documentPath(p *Path, specialPaths *logical.Paths, backendType logical.Back | ||||
|  | ||||
| 				// Set the final request body. Only JSON request data is supported. | ||||
| 				if len(s.Properties) > 0 || s.Example != nil { | ||||
| 					requestName := constructRequestName(requestResponsePrefix, path) | ||||
| 					doc.Components.Schemas[requestName] = s | ||||
| 					op.RequestBody = &OASRequestBody{ | ||||
| 						Content: OASContent{ | ||||
| 							"application/json": &OASMediaTypeObject{ | ||||
| 								Schema: s, | ||||
| 								Schema: &OASSchema{Ref: fmt.Sprintf("#/components/schemas/%s", requestName)}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					} | ||||
| @@ -459,6 +470,30 @@ func documentPath(p *Path, specialPaths *logical.Paths, backendType logical.Back | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // constructRequestName joins the given prefix with the path elements into a | ||||
| // CamelCaseRequest string. | ||||
| // | ||||
| // For example, prefix="kv" & path=/config/lease/{name} => KvConfigLeaseRequest | ||||
| func constructRequestName(requestResponsePrefix string, path string) string { | ||||
| 	var b strings.Builder | ||||
|  | ||||
| 	b.WriteString(strings.Title(requestResponsePrefix)) | ||||
|  | ||||
| 	// split the path by / _ - separators | ||||
| 	for _, token := range strings.FieldsFunc(path, func(r rune) bool { | ||||
| 		return r == '/' || r == '_' || r == '-' | ||||
| 	}) { | ||||
| 		// exclude request fields | ||||
| 		if !strings.ContainsAny(token, "{}") { | ||||
| 			b.WriteString(strings.Title(token)) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	b.WriteString("Request") | ||||
|  | ||||
| 	return b.String() | ||||
| } | ||||
|  | ||||
| func specialPathMatch(path string, specialPaths []string) bool { | ||||
| 	// Test for exact or prefix match of special paths. | ||||
| 	for _, sp := range specialPaths { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user