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

error generating code: error formatting Go code -> expected operand, found '<' (and 10 more errors)

Open mat813 opened this issue 1 year ago • 2 comments

I am trying to generate the code for netbox, and it has a few schemas like this :

components:
  schemas:
    ConsolePort:
      type: object
        speed:
          type: object
          properties:
            value:
              enum:
              - 1200
              - 2400
              - 4800
              - 9600
              - 19200
              - 38400
              - 57600
              - 115200
              - null
              type: integer
              description: |-
                * `1200` - 1200 bps
                * `2400` - 2400 bps
                * `4800` - 4800 bps
                * `9600` - 9600 bps
                * `19200` - 19.2 kbps
                * `38400` - 38.4 kbps
                * `57600` - 57.6 kbps
                * `115200` - 115.2 kbps
            label:
              type: string
              enum:
              - 1200 bps
              - 2400 bps
              - 4800 bps
              - 9600 bps
              - 19.2 kbps
              - 38.4 kbps
              - 57.6 kbps
              - 115.2 kbps
          nullable: true

where the values list has a null in it, and the generated code is:

// Defines values for ConsolePortSpeedValue.
const (

  ConsolePortSpeedValueN115200 ConsolePortSpeedValue = 115200
  ConsolePortSpeedValueN1200 ConsolePortSpeedValue = 1200
  ConsolePortSpeedValueN19200 ConsolePortSpeedValue = 19200
  ConsolePortSpeedValueN2400 ConsolePortSpeedValue = 2400
  ConsolePortSpeedValueN38400 ConsolePortSpeedValue = 38400
  ConsolePortSpeedValueN4800 ConsolePortSpeedValue = 4800
  ConsolePortSpeedValueN57600 ConsolePortSpeedValue = 57600
  ConsolePortSpeedValueN9600 ConsolePortSpeedValue = 9600
  ConsolePortSpeedValueNil ConsolePortSpeedValue = <nil>
)

Note the <nil> last value. I figure it should be simply nil, or maybe be skipped. For now, I edited the yaml definition to remove all null values from enum.

mat813 avatar Aug 23 '24 09:08 mat813

Thanks for the report! I can't remember if this is allowed in 3.0.x (it doesn't seem to flag a violation with vacuum) but we can see if it's allowed, and if so try and infer whether we should be a nullable: true in this case

jamietanna avatar Aug 25 '24 09:08 jamietanna

There is docs about nullable. Is the following code look like you expect?

// Defines values for ConsolePortSpeedValue.
func nullable[T any](v T) *T {
	return &v
}

// ConsolePortSpeedValue * `1200` - 1200 bps
// * `2400` - 2400 bps
// * `4800` - 4800 bps
// * `9600` - 9600 bps
// * `19200` - 19.2 kbps
// * `38400` - 38.4 kbps
// * `57600` - 57.6 kbps
// * `115200` - 115.2 kbps
type ConsolePortSpeedValue *int

var (
	N115200 ConsolePortSpeedValue = nullable(115200)
	N1200   ConsolePortSpeedValue = nullable(1200)
	N19200  ConsolePortSpeedValue = nullable(19200)
	N2400   ConsolePortSpeedValue = nullable(2400)
	N38400  ConsolePortSpeedValue = nullable(38400)
	N4800   ConsolePortSpeedValue = nullable(4800)
	N57600  ConsolePortSpeedValue = nullable(57600)
	N9600   ConsolePortSpeedValue = nullable(9600)
	Nil     ConsolePortSpeedValue = nil
)

chenyunda218 avatar Oct 10 '24 06:10 chenyunda218