Пример #1
0
func setParameterType(s *spec.Parameter, f *descriptor.Field) {
	if f.GetLabel() == godesc.FieldDescriptorProto_LABEL_REPEATED {
		s.Type = "array"
		items := new(spec.Items)
		if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
			items.Ref = spec.MustCreateRef("#/definitions/" + f.GetTypeName())
		} else {
			ty, format := toSwaggerType(f.GetType())
			items.Type = ty
			items.Format = format
		}

		s.Items = items
		return
	}

	if f.GetType() == godesc.FieldDescriptorProto_TYPE_MESSAGE {
		s.Ref = spec.MustCreateRef("#/definitions/" + f.GetTypeName())
		return
	}

	ty, format := toSwaggerType(f.GetType())
	s.Type = ty
	s.Format = format
}
Пример #2
0
func validateRequiredAllowEmptyTest(t *testing.T, param *spec.Parameter, value reflect.Value) {
	param.AllowEmptyValue = true
	binder := np(param)
	err := binder.bindValue([]string{}, true, value)
	assert.NoError(t, err)
	if assert.NotNil(t, param) {
		err = binder.bindValue([]string{""}, true, value)
		assert.NoError(t, err)
		err = binder.bindValue([]string{"1"}, false, value)
		assert.Error(t, err)
		assert.EqualError(t, requiredError(param), err.Error())
		err = binder.bindValue([]string{""}, false, value)
		assert.Error(t, err)
		assert.EqualError(t, requiredError(param), err.Error())
	}
}
Пример #3
0
func (g *Generator) generatePathParameter(param *descriptor.Parameter) *spec.Parameter {
	p := new(spec.Parameter)
	p.Name = param.String()
	// TODO(ceram1): ?
	p.Required = true
	if param.Target.DefaultValue != nil {
		p.Default = param.Target.GetDefaultValue()
	}
	if cmt := g.file.GetCommentText(param.Target.CommentPath); cmt != nil {
		p.Description = *cmt
	}

	p.In = "path"

	setParameterType(p, param.Target)

	return p
}
Пример #4
0
func buildOperation(route *revel.Route) *spec.Operation {
	var (
		typeInfo   *harness.TypeInfo
		methodSpec *harness.MethodSpec
	)

	info, rerr := harness.ProcessSource(revel.CodePaths)
	if rerr != nil {
		panic(rerr) // TODO EMPTY PANIC
	}

	// get the TypeInfo and MethodSpec for this route
	for _, cinfo := range info.ControllerSpecs() {
		typeInfo = cinfo // TODO move inside if (get around compiler complaint)
		if route.ControllerName == typeInfo.StructName {
			for _, spec := range cinfo.MethodSpecs {
				if route.MethodName == spec.Name {
					methodSpec = spec
					break
				}
			}
			break
		}
	}

	op := new(spec.Operation)
	// TODO op.Description
	// this will probably require either editing harness.ProcessSource to also grab comments OR
	// to copy that functionality and modify it
	op.Consumes = ContentTypes
	op.Produces = ContentTypes
	op.AddExtension("x-revel-action", route.Action)

	for i, arg := range methodSpec.Args {
		// skip over fixed paramters that match up to the arguments
		if i < len(route.FixedParams) {
			continue
		}
		var param spec.Parameter
		param.Name = arg.Name
		param.Type = arg.TypeExpr.Expr

		// TODO review
		// TODO: better path vs query vs body vs multipart
		count := strings.Count(route.Path, ":") + strings.Count(route.Path, "*")
		if i < count {
			param.In = "path"
		} else {
			param.In = "body"
		}
		op.Parameters = append(op.Parameters, param)
	}

	// TODO RenderCalls
	// fmt.Printf("route:       %#v\n", route)
	// fmt.Printf("typeInfo:    %#v\n", typeInfo)
	// fmt.Printf("methodSpec: %#v\n", methodSpec)
	// for _, call := range methodSpec.RenderCalls {
	// 	fmt.Printf("\tcall: %#v\n", call)
	// }

	/*
		"responses": {
		  "200": {
		    "description": "A list of pets.",
		    "schema": {
		      "type": "array",
		      "items": {
		        "$ref": "#/definitions/Pet"
		      }
		    }
		  }
		}
	*/

	return op
}