mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 00:07:50 +00:00
Support optional tag in OpenAPI generator
This commit is contained in:
parent
25afcc5522
commit
c1096211d0
@ -32,7 +32,7 @@ type TestType struct {
|
|||||||
// +optional
|
// +optional
|
||||||
api.ObjectMeta `json:"metadata,omitempty"`
|
api.ObjectMeta `json:"metadata,omitempty"`
|
||||||
// +optional
|
// +optional
|
||||||
Status TestTypeStatus `json:"status,omitempty"`
|
Status TestTypeStatus `json:"status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestTypeList struct {
|
type TestTypeList struct {
|
||||||
|
@ -36,6 +36,7 @@ import (
|
|||||||
|
|
||||||
// This is the comment tag that carries parameters for open API generation.
|
// This is the comment tag that carries parameters for open API generation.
|
||||||
const tagName = "k8s:openapi-gen"
|
const tagName = "k8s:openapi-gen"
|
||||||
|
const tagOptional = "optional"
|
||||||
|
|
||||||
// Known values for the tag.
|
// Known values for the tag.
|
||||||
const (
|
const (
|
||||||
@ -56,6 +57,11 @@ func hasOpenAPITagValue(comments []string, value string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasOptionalTag(comments []string) bool {
|
||||||
|
tagValues := types.ExtractCommentTags("+", comments)[tagOptional]
|
||||||
|
return tagValues != nil
|
||||||
|
}
|
||||||
|
|
||||||
// NameSystems returns the name system used by the generators in this package.
|
// NameSystems returns the name system used by the generators in this package.
|
||||||
func NameSystems() namer.NameSystems {
|
func NameSystems() namer.NameSystems {
|
||||||
return namer.NameSystems{
|
return namer.NameSystems{
|
||||||
@ -225,30 +231,6 @@ func getReferableName(m *types.Member) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func optionIndex(s, optionName string) int {
|
|
||||||
ret := 0
|
|
||||||
for s != "" {
|
|
||||||
var next string
|
|
||||||
i := strings.Index(s, ",")
|
|
||||||
if i >= 0 {
|
|
||||||
s, next = s[:i], s[i+1:]
|
|
||||||
}
|
|
||||||
if s == optionName {
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
s = next
|
|
||||||
ret++
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func isPropertyRequired(m *types.Member) bool {
|
|
||||||
// A property is required if it does not have omitempty value in its json tag (documentation and implementation
|
|
||||||
// of json package requires omitempty to be at location 1 or higher.
|
|
||||||
// TODO: Move optional field definition from tags to comments.
|
|
||||||
return optionIndex(reflect.StructTag(m.Tags).Get("json"), "omitempty") < 1
|
|
||||||
}
|
|
||||||
|
|
||||||
type openAPITypeWriter struct {
|
type openAPITypeWriter struct {
|
||||||
*generator.SnippetWriter
|
*generator.SnippetWriter
|
||||||
refTypes map[string]*types.Type
|
refTypes map[string]*types.Type
|
||||||
@ -306,7 +288,7 @@ func (g openAPITypeWriter) generate(t *types.Type) error {
|
|||||||
if name == "" {
|
if name == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if isPropertyRequired(&m) {
|
if !hasOptionalTag(m.CommentLines) {
|
||||||
required = append(required, name)
|
required = append(required, name)
|
||||||
}
|
}
|
||||||
if err := g.generateProperty(&m); err != nil {
|
if err := g.generateProperty(&m); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user