@@ -127,19 +127,17 @@ const (
127127type openAPIGen struct {
128128 generator.GoGenerator
129129 // TargetPackage is the package that will get GetOpenAPIDefinitions function returns all open API definitions.
130- targetPackage string
131- imports namer.ImportTracker
132- useGeneratedModelNames bool
130+ targetPackage string
131+ imports namer.ImportTracker
133132}
134133
135- func newOpenAPIGen (outputFilename string , targetPackage string , useGeneratedModelNames bool ) generator.Generator {
134+ func newOpenAPIGen (outputFilename string , targetPackage string ) generator.Generator {
136135 return & openAPIGen {
137136 GoGenerator : generator.GoGenerator {
138137 OutputFilename : outputFilename ,
139138 },
140- imports : generator .NewImportTrackerForPackage (targetPackage ),
141- targetPackage : targetPackage ,
142- useGeneratedModelNames : useGeneratedModelNames ,
139+ imports : generator .NewImportTrackerForPackage (targetPackage ),
140+ targetPackage : targetPackage ,
143141 }
144142}
145143
@@ -181,7 +179,7 @@ func (g *openAPIGen) Init(c *generator.Context, w io.Writer) error {
181179 sw .Do ("return map[string]$.OpenAPIDefinition|raw${\n " , argsFromType (nil ))
182180
183181 for _ , t := range c .Order {
184- err := newOpenAPITypeWriter (sw , c , g . useGeneratedModelNames ).generateCall (t )
182+ err := newOpenAPITypeWriter (sw , c ).generateCall (t )
185183 if err != nil {
186184 return err
187185 }
@@ -196,7 +194,7 @@ func (g *openAPIGen) Init(c *generator.Context, w io.Writer) error {
196194func (g * openAPIGen ) GenerateType (c * generator.Context , t * types.Type , w io.Writer ) error {
197195 klog .V (5 ).Infof ("generating for type %v" , t )
198196 sw := generator .NewSnippetWriter (w , c , "$" , "$" )
199- err := newOpenAPITypeWriter (sw , c , g . useGeneratedModelNames ).generate (t )
197+ err := newOpenAPITypeWriter (sw , c ).generate (t )
200198 if err != nil {
201199 return err
202200 }
@@ -235,16 +233,14 @@ type openAPITypeWriter struct {
235233 refTypes map [string ]* types.Type
236234 enumContext * enumContext
237235 GetDefinitionInterface * types.Type
238- useGeneratedModelNames bool
239236}
240237
241- func newOpenAPITypeWriter (sw * generator.SnippetWriter , c * generator.Context , useGeneratedModelNames bool ) openAPITypeWriter {
238+ func newOpenAPITypeWriter (sw * generator.SnippetWriter , c * generator.Context ) openAPITypeWriter {
242239 return openAPITypeWriter {
243- SnippetWriter : sw ,
244- context : c ,
245- refTypes : map [string ]* types.Type {},
246- enumContext : newEnumContext (c ),
247- useGeneratedModelNames : useGeneratedModelNames ,
240+ SnippetWriter : sw ,
241+ context : c ,
242+ refTypes : map [string ]* types.Type {},
243+ enumContext : newEnumContext (c ),
248244 }
249245}
250246
@@ -299,6 +295,40 @@ func hasOpenAPIV3OneOfMethod(t *types.Type) bool {
299295 return false
300296}
301297
298+ func hasOpenAPIModelName (t * types.Type ) bool {
299+ for mn , mt := range t .Methods {
300+ if mn != "OpenAPIModelName" {
301+ continue
302+ }
303+ return methodReturnsValue (mt , "" , "string" )
304+ }
305+ return false
306+ }
307+
308+ func (g openAPITypeWriter ) shouldUseOpenAPIModelName (t * types.Type ) bool {
309+ // Finds non-generated OpenAPIModelName() functions.
310+ // Generated OpenAPIModelName() are ignored due to the 'ignore_autogenerated' build tag
311+ // but are handled below by checking for use of the +k8s:openapi-model-package.
312+ // This approach allows code generators to be called in any order.
313+ if hasOpenAPIModelName (t ) {
314+ return true
315+ }
316+
317+ value , err := extractOpenAPISchemaNamePackage (t .CommentLines )
318+ if err != nil {
319+ klog .Fatalf ("Type %v: invalid %s:%v" , t , tagModelPackage , err )
320+ }
321+ if value != "" {
322+ return true
323+ }
324+ pkg := g .context .Universe .Package (t .Name .Package )
325+ value , err = extractOpenAPISchemaNamePackage (pkg .Comments )
326+ if err != nil {
327+ klog .Fatalf ("Package %v: invalid %s:%v" , pkg , tagModelPackage , err )
328+ }
329+ return value != ""
330+ }
331+
302332// typeShortName returns short package name (e.g. the name x appears in package x definition) dot type name.
303333func typeShortName (t * types.Type ) string {
304334 // `path` vs. `filepath` because packages use '/'
@@ -349,7 +379,7 @@ func (g openAPITypeWriter) generateCall(t *types.Type) error {
349379
350380 args := argsFromType (t )
351381
352- if g .useGeneratedModelNames {
382+ if g .shouldUseOpenAPIModelName ( t ) {
353383 g .Do ("$.|raw${}.OpenAPIModelName(): " , t )
354384 } else {
355385 // Legacy case: use the "canonical type name"
@@ -685,7 +715,7 @@ func (g openAPITypeWriter) generate(t *types.Type) error {
685715 g .Do ("Dependencies: []string{\n " , args )
686716 for _ , k := range deps {
687717 t := g .refTypes [k ]
688- if g .useGeneratedModelNames {
718+ if g .shouldUseOpenAPIModelName ( t ) {
689719 g .Do ("$.|raw${}.OpenAPIModelName()," , t )
690720 } else {
691721 g .Do ("\" $.$\" ," , k )
@@ -1051,7 +1081,7 @@ func (g openAPITypeWriter) generateSimpleProperty(typeString, format string) {
10511081
10521082func (g openAPITypeWriter ) generateReferenceProperty (t * types.Type ) {
10531083 g .refTypes [t .Name .String ()] = t
1054- if g .useGeneratedModelNames {
1084+ if g .shouldUseOpenAPIModelName ( t ) {
10551085 g .Do ("Ref: ref($.|raw${}.OpenAPIModelName()),\n " , t )
10561086 } else {
10571087 g .Do ("Ref: ref(\" $.$\" ),\n " , t .Name .String ())
0 commit comments