oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Non-alias type for specific schema when needed

Open sparshev opened this issue 3 years ago • 1 comments

Hello folks,

I want to define a type not as alias to attach GORM Scanner/Valuer functions to properly serialize/deserialize it in DB as json document:

  • input:
    components:
      schemas:
        LabelDefinitions:
          type: array
          items:
            $ref: '#/components/schemas/LabelDefinition'
    
  • desired output:
    type LabelDefinitions []LabelDefinition
    
  • actual output:
    type LabelDefinitions = []LabelDefinition
    

But I did not found a way to do that and seems Schema.DefineViaAlias controlling the Schema's IsAlias() which is defined in template pkg/codegen/templates/typedef.tmpl type {{.TypeName}} {{if .IsAlias }}={{end}} {{.Schema.TypeDecl}}. And this leads to the next error, because it's not a type but alias:

  • Func definition
    func (ld LabelDefinitions) GormDataType() string {
        return "blob"
    }
    ...
    
  • Result:
    label_definitions.go: invalid receiver type []LabelDefinition
    

Workarounds:

  • I found compatibility:old-aliasing option, but it looks like not a good idea to set it for the entire types generation for the future...
  • For now I made a bit of post-processing in the build script generation section to remove " = " for this particular case, but I wonder if there's the other options I have?

Thank you

sparshev avatar Nov 16 '22 03:11 sparshev