@@ -88,6 +88,8 @@ type schemaModel struct {
8888 Fields []schemaField
8989 Imports []string
9090 HasRequired bool
91+ IsEnum bool
92+ EnumValues []enumValueModel
9193}
9294
9395// schemaField stores metadata for a field within a schemaModel.
@@ -98,6 +100,12 @@ type schemaField struct {
98100 Required bool
99101}
100102
103+ // enumValueModel captures a single enum constant and its wire value.
104+ type enumValueModel struct {
105+ Name string
106+ WireValue string
107+ }
108+
101109const (
102110 parameterInPath = "path"
103111 parameterInQuery = "query"
@@ -507,8 +515,24 @@ func buildSchemas(doc *v3.Document, params Params, resolver *typeResolver) []sch
507515 if ref == nil {
508516 continue
509517 }
510- fields , imports , hasRequired := buildSchemaFields (name , ref , resolver )
511518 description := schemaDescription (ref )
519+ if enumValues , ok := enumValuesForSchema (schemaFromProxy (ref )); ok {
520+ imports := sortedImports (map [string ]struct {}{
521+ "com.fasterxml.jackson.annotation.JsonCreator" : {},
522+ "com.fasterxml.jackson.annotation.JsonValue" : {},
523+ })
524+ result = append (result , schemaModel {
525+ Name : name ,
526+ ClassName : pascalCase (name , "" ),
527+ Package : params .modelPackage (),
528+ DescriptionLines : splitComment (description ),
529+ Imports : imports ,
530+ IsEnum : true ,
531+ EnumValues : enumValues ,
532+ })
533+ continue
534+ }
535+ fields , imports , hasRequired := buildSchemaFields (name , ref , resolver )
512536 if hasRequired {
513537 imports = uniqueStrings (append (imports , "java.util.Objects" ))
514538 }
@@ -587,6 +611,51 @@ func buildSchemaFields(name string, ref *base.SchemaProxy, resolver *typeResolve
587611 return fields , sortedImports (imports ), hasRequired
588612}
589613
614+ // enumValuesForSchema extracts enum values for string schemas.
615+ func enumValuesForSchema (schema * base.Schema ) ([]enumValueModel , bool ) {
616+ if schema == nil || len (schema .Enum ) == 0 {
617+ return nil , false
618+ }
619+ if len (schema .Type ) > 0 && ! schemaHasType (schema , "string" ) {
620+ return nil , false
621+ }
622+ if len (schema .Type ) == 0 && ! schemaHasType (schema , "string" ) {
623+ for _ , node := range schema .Enum {
624+ if node == nil || node .Tag != "!!str" {
625+ return nil , false
626+ }
627+ }
628+ }
629+
630+ values := make ([]enumValueModel , 0 , len (schema .Enum ))
631+ usedNames := map [string ]int {}
632+ for _ , node := range schema .Enum {
633+ if node == nil {
634+ continue
635+ }
636+ raw := node .Value
637+ if raw == "" {
638+ var decoded string
639+ if err := node .Decode (& decoded ); err == nil {
640+ raw = decoded
641+ }
642+ }
643+ name := enumConstantName (raw )
644+ if count := usedNames [name ]; count > 0 {
645+ name = fmt .Sprintf ("%s_%d" , name , count + 1 )
646+ }
647+ usedNames [name ] = usedNames [name ] + 1
648+ values = append (values , enumValueModel {
649+ Name : name ,
650+ WireValue : raw ,
651+ })
652+ }
653+ if len (values ) == 0 {
654+ return nil , false
655+ }
656+ return values , true
657+ }
658+
590659// collectProperties gathers direct and allOf properties into a unified map.
591660func collectProperties (schema * base.Schema ) map [string ]* base.SchemaProxy {
592661 props := map [string ]* base.SchemaProxy {}
0 commit comments