Exemple #1
0
func setSchemaType(s *spec.Schema, f *descriptor.Field) {
	if f.GetLabel() == godesc.FieldDescriptorProto_LABEL_REPEATED {
		s.Type = spec.StringOrArray([]string{"array"})

		items := new(spec.Schema)
		if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
			items.Ref = messageRef(f.Message)
		} else {
			ty, format := toSwaggerType(f.GetType())
			items.Type = spec.StringOrArray([]string{ty})
			items.Format = format
		}

		s.Items = &spec.SchemaOrArray{Schema: items}
		return
	}

	if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
		s.Ref = messageRef(f.Message)
		return
	}

	ty, format := toSwaggerType(f.GetType())
	s.Type = spec.StringOrArray([]string{ty})
	s.Format = format
}
Exemple #2
0
func (scp *schemaParser) parseAllOfMember(gofile *ast.File, schema *spec.Schema, expr ast.Expr, seenPreviously map[string]struct{}) error {
	// TODO: check if struct is annotated with swagger:model or known in the definitions otherwise
	var pkg *loader.PackageInfo
	var file *ast.File
	var gd *ast.GenDecl
	var ts *ast.TypeSpec
	var err error

	switch tpe := expr.(type) {
	case *ast.Ident:
		// do lookup of type
		// take primitives into account, they should result in an error for swagger
		pkg, err = scp.packageForFile(gofile)
		if err != nil {
			return err
		}
		file, gd, ts, err = findSourceFile(pkg, tpe.Name)
		if err != nil {
			return err
		}

	case *ast.SelectorExpr:
		// look up package, file and then type
		pkg, err = scp.packageForSelector(gofile, tpe.X)
		if err != nil {
			return fmt.Errorf("embedded struct: %v", err)
		}
		file, gd, ts, err = findSourceFile(pkg, tpe.Sel.Name)
		if err != nil {
			return fmt.Errorf("embedded struct: %v", err)
		}
	default:
		return fmt.Errorf("unable to resolve allOf member for: %v\n", expr)
	}

	sd := newSchemaDecl(file, gd, ts)
	if sd.hasAnnotation() {
		ref, err := spec.NewRef("#/definitions/" + sd.Name)
		if err != nil {
			return err
		}
		schema.Ref = ref
		scp.postDecls = append(scp.postDecls, *sd)
	} else {
		switch st := ts.Type.(type) {
		case *ast.StructType:
			return scp.parseStructType(file, schema, st, seenPreviously)
		case *ast.InterfaceType:
			return scp.parseInterfaceType(file, schema, st, seenPreviously)
		}
	}

	return nil
}