Ejemplo n.º 1
0
func structToSchema(pidl *idl.Idl, st *idl.Struct) *swagger2.Schema {
	sc := new(swagger2.Schema)
	// sc.Title
	sc.Description = strings.Join(st.Comments, "\n")
	sc.Properties = make(map[string]swagger2.Schema)
	sc.Type = "object"
	for _, p := range st.Fields {
		sc.Properties[p.Name] = *fieldToSchema(pidl, p)
	}
	if st.Extends != "" {
		sc.AllOf = make([]swagger2.Schema, 0)

		// SWAGGER-BUG: swagger-js and swagger-ui don't support allOf. Alternate implementation below
		// see https://github.com/swagger-api/swagger-js/issues/188
		if !flatten {
			sc.AllOf = append(sc.AllOf, swagger2.Schema{ItemsDef: swagger2.ItemsDef{Ref: "#/definitions/" + st.Extends}})

			// Attach myself as second schema in AllOf
			sc2 := new(swagger2.Schema)
			sc2.Properties = sc.Properties
			sc2.Type = "object"
			sc.Type = ""
			sc.Properties = nil
			sc.AllOf = append(sc.AllOf, *sc2)
		} else {
			// Alternate implementation due to swagger bug
			bases, err := st.BaseClasses(pidl)
			if err != nil {
				panic("cannot get base classes")
			}
			// add base properties
			for _, b := range bases {
				for _, p := range b.Fields {
					sc.Properties[p.Name] = *fieldToSchema(pidl, p)
				}
			}
		}
	}

	return sc
}
Ejemplo n.º 2
0
func parmsToSchema(pidl *idl.Idl, m *idl.Method) *swagger2.Schema {
	sc := new(swagger2.Schema)
	// sc.Title
	sc.Description = strings.Join(m.Comments, "\n")
	sc.Properties = make(map[string]swagger2.Schema)
	sc.Type = "object"
	if m.HasParameters() {
		for _, p := range m.Parameters {
			sc.Properties[p.Name] = *fieldToSchema(pidl, p)
		}
	}
	return sc
}