8000 Broken model code generated for polymorphic swagger types that have lists of non-concrete types. · Issue #607 · go-swagger/go-swagger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Broken model code generated for polymorphic swagger types that have lists of non-concrete types. #607
Closed
@robotoer

Description

@robotoer

Problem statement

Broken model code seems to be produced when an extended type (AndFilter) has a property (config) that is a list of a discriminator/polymorphic base type (Filter).

Swagger specification

definitions:
  RangeFilter:
    allOf:
    - $ref: '#/definitions/Filter'
    - properties:
        config:
          properties:
            gt:
              format: double
              type: number
            gte:
              format: double
              type: number
            lt:
              format: double
              type: number
            lte:
              format: double
              type: number
          type: object
      required:
      - config
      type: object
  Filter:
    discriminator: type
    properties:
      type:
        type: string
    required:
    - type
    type: object
  AndFilter:
    allOf:
    - $ref: '#/definitions/Filter'
    - properties:
        config:
          items:
            $ref: '#/definitions/Filter'
          type: array
      required:
      - config
      type: object

Steps to reproduce

$ swagger generate client -f ../v2.yaml 
2016/08/01 17:51:06 building a plan for generation
2016/08/01 17:51:06 planning definitions
2016/08/01 17:51:06 planning operations
2016/08/01 17:51:06 grouping operations into packages
2016/08/01 17:51:06 planning meta data and facades
20
5FAB
16/08/01 17:51:06 rendered model template: Filter
2016/08/01 17:51:06 rendered model template: AndFilter
2016/08/01 17:51:06 models/and_filter.go:95:34: expected operand, found ']' (and 4 more errors)
2016/08/01 17:51:06 rendered model template: RangeFilter
2016/08/01 17:51:06 rendered client facade template: client.RestClient
robert@R90GVSMX:~/gocode/src/code.earth.planet.com/robert.chu
$ cd models/
robert@R90GVSMX:~/gocode/src/code.earth.planet.com/robert.chu/models
$ ls
and_filter.go  filter.go  range_filter.go
robert@R90GVSMX:~/gocode/src/code.earth.planet.com/robert.chu/models
$ go build
# code.earth.planet.com/robert.chu/models
./and_filter.go:95: syntax error: unexpected ], expecting expression
./and_filter.go:95: missing index in index expression
robert@R90GVSMX:~/gocode/src/code.earth.planet.com/robert.chu/models
$ cat and_filter.go
package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
  strfmt "github.com/go-openapi/strfmt"

  "github.com/go-openapi/errors"
  "github.com/go-openapi/runtime"
  "github.com/go-openapi/validate"


)



/*AndFilter and filter

swagger:model AndFilter
*/
type AndFilter struct {








  configField []Filter








}



func (m *AndFilter) Type() string{
  return "AndFilter"
}
func (m *AndFilter) SetType(val string) {

}


func (m *AndFilter) Config() []Filter{
  return m.configField
}
func (m *AndFilter) SetConfig(val []Filter) {
  m.configField = val
}




// UnmarshalJSON unmarshals this object with a polymorphic type from a JSON structure
func (m *AndFilter) UnmarshalJSON(raw []byte) error {
  var data struct {



Type string `json:"type"`







Config []Filter `json:"config"`








  }

  buf := bytes.NewBuffer(raw)
  dec := json.NewDecoder(buf)
  dec.UseNumber()

  if err := dec.Decode(&data); err != nil {
        return err
  }

  allOf0Config, err := Unmarshal[]Filter(bytes.NewBuffer(raw), runtime.JSONConsumer())
  if err != nil {
    return err
  }



  var result AndFilter
  result.configField = allOf0Config
  *m = result
  return nil
}

// MarshalJSON marshals this object with a polymorphic type to a JSON structure
func (m AndFilter) MarshalJSON() ([]byte, error) { 
        var b1, b2 []byte
        var err error
        b1, err = json.Marshal(struct {



Type string `json:"type"`







Config []Filter `json:"config"`








}{

  })
        if err != nil {
                return nil, err
        }
        b2, err = json.Marshal(struct{ 
    Config []Filter `json:"config"`
  }{ 
    Config: m.configField,
  })
        if err != nil {
                return nil, err
        }
        return swag.ConcatJSON(b1, b2), nil
}


// Validate validates this and filter
func (m *AndFilter) Validate(formats strfmt.Registry) error {
  var res []error
















    if err := m.validateConfig(formats); err != nil {
      res = append(res, err)
    }












  if len(res) > 0 {
    return errors.CompositeValidationError(res...)
  }
  return nil
}










func (m *AndFilter) validateConfig(formats strfmt.Registry) error {



if err := validate.Required("config", "body", m.Config); err != nil {
  return err
}






for i := 0; i < len(m.Config); i++ {






if err := m.Config[i].Validate(formats); err != nil {
  return err
}




}




  return nil
}









Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdiscriminatorRelated to polymorphic typesmodelRelated to swagger generate model command

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0