Skip to content

Commit

Permalink
Merge pull request #1311 from microsoft/bugfix/go-tpl-params
Browse files Browse the repository at this point in the history
- Fixed a bug in Go generator where temporary url template parameters would not be used preventing the use of raw urls
  • Loading branch information
baywet authored Mar 3, 2022
2 parents 2da31d2 + 49c3f0e commit de55f78
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed a bug in Go generator where temporary url template parameters would not be used preventing the use of raw urls.
- Fixed a bug where the Go http client configuration would impact non-kiota requests.
- Fixed bug where installing python abstractions failed due to missing dependencies #1289
- Modified python test matrix to include python 3.10 #1289
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ type AnonymousAuthenticationProvider struct {
}

// AuthenticateRequest is a placeholder method that "authenticates" the RequestInformation instance: no-op.
func (provider *AnonymousAuthenticationProvider) AuthenticateRequest(request abs.RequestInformation) error {
func (provider *AnonymousAuthenticationProvider) AuthenticateRequest(request *abs.RequestInformation) error {
return nil
}
2 changes: 1 addition & 1 deletion abstractions/go/authentication/authentication_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
// AuthenticationProvider authenticates the RequestInformation request.
type AuthenticationProvider interface {
// AuthenticateRequest authenticates the provided RequestInformation.
AuthenticateRequest(request abs.RequestInformation) error
AuthenticateRequest(request *abs.RequestInformation) error
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func NewBaseBearerTokenAuthenticationProvider(accessTokenProvider AccessTokenPro
}

// AuthenticateRequest authenticates the provided RequestInformation instance using the provided authorization token callback.
func (provider *BaseBearerTokenAuthenticationProvider) AuthenticateRequest(request abs.RequestInformation) error {
func (provider *BaseBearerTokenAuthenticationProvider) AuthenticateRequest(request *abs.RequestInformation) error {
if request == nil {
return errors.New("request is nil")
}
if request.Headers == nil {
request.Headers = make(map[string]string)
}
Expand Down
10 changes: 5 additions & 5 deletions abstractions/go/request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ type ErrorMappings map[string]s.ParsableFactory
// RequestAdapter is the service responsible for translating abstract RequestInformation into native HTTP requests.
type RequestAdapter interface {
// SendAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model.
SendAsync(requestInfo RequestInformation, constructor s.ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) (s.Parsable, error)
SendAsync(requestInfo *RequestInformation, constructor s.ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) (s.Parsable, error)
// SendCollectionAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection.
SendCollectionAsync(requestInfo RequestInformation, constructor s.ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) ([]s.Parsable, error)
SendCollectionAsync(requestInfo *RequestInformation, constructor s.ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) ([]s.Parsable, error)
// SendPrimitiveAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model.
SendPrimitiveAsync(requestInfo RequestInformation, typeName string, responseHandler ResponseHandler, errorMappings ErrorMappings) (interface{}, error)
SendPrimitiveAsync(requestInfo *RequestInformation, typeName string, responseHandler ResponseHandler, errorMappings ErrorMappings) (interface{}, error)
// SendPrimitiveCollectionAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model collection.
SendPrimitiveCollectionAsync(requestInfo RequestInformation, typeName string, responseHandler ResponseHandler, errorMappings ErrorMappings) ([]interface{}, error)
SendPrimitiveCollectionAsync(requestInfo *RequestInformation, typeName string, responseHandler ResponseHandler, errorMappings ErrorMappings) ([]interface{}, error)
// SendNoContentAsync executes the HTTP request specified by the given RequestInformation with no return content.
SendNoContentAsync(requestInfo RequestInformation, responseHandler ResponseHandler, errorMappings ErrorMappings) error
SendNoContentAsync(requestInfo *RequestInformation, responseHandler ResponseHandler, errorMappings ErrorMappings) error
// GetSerializationWriterFactory returns the serialization writer factory currently in use for the request adapter service.
GetSerializationWriterFactory() s.SerializationWriterFactory
// EnableBackingStore enables the backing store proxies for the SerializationWriters and ParseNodes in use.
Expand Down
13 changes: 13 additions & 0 deletions abstractions/go/request_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ func TestItAddsStringArrayQueryParameters(t *testing.T) {
requestInformation.AddQueryParameters(queryParameters)
assert.Equal(t, "somefilter,someotherfilter", requestInformation.QueryParameters["Expand"])
}

func TestItSetsTheRawURL(t *testing.T) {
requestInformation := NewRequestInformation()
requestInformation.PathParameters[raw_url_key] = "https://someurl.com"
requestInformation.UrlTemplate = "https://someotherurl.com{?select}"
requestInformation.AddQueryParameters(QueryParameters{
Select_escaped: []string{"somefield", "somefield2"},
})
uri, err := requestInformation.GetUri()
assert.Nil(t, err)
assert.Equal(t, "https://someurl.com", uri.String())
assert.Equal(t, 0, len(requestInformation.QueryParameters))
}
31 changes: 23 additions & 8 deletions http/go/nethttp/nethttp_request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (a *NetHttpRequestAdapter) SetBaseUrl(baseUrl string) {
func (a *NetHttpRequestAdapter) GetBaseUrl() string {
return a.baseUrl
}
func (a *NetHttpRequestAdapter) getHttpResponseMessage(requestInfo abs.RequestInformation) (*nethttp.Response, error) {
func (a *NetHttpRequestAdapter) getHttpResponseMessage(requestInfo *abs.RequestInformation) (*nethttp.Response, error) {
a.setBaseUrlForRequestInformation(requestInfo)
err := a.authenticationProvider.AuthenticateRequest(requestInfo)
if err != nil {
Expand All @@ -108,10 +108,10 @@ func (a *NetHttpRequestAdapter) getResponsePrimaryContentType(response *nethttp.
splat := strings.Split(rawType, ";")
return strings.ToLower(splat[0])
}
func (a *NetHttpRequestAdapter) setBaseUrlForRequestInformation(requestInfo abs.RequestInformation) {
func (a *NetHttpRequestAdapter) setBaseUrlForRequestInformation(requestInfo *abs.RequestInformation) {
requestInfo.PathParameters["baseurl"] = a.GetBaseUrl()
}
func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(requestInfo abs.RequestInformation) (*nethttp.Request, error) {
func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(requestInfo *abs.RequestInformation) (*nethttp.Request, error) {
uri, err := requestInfo.GetUri()
if err != nil {
return nil, err
Expand Down Expand Up @@ -139,7 +139,10 @@ func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(requestInfo abs
}

// SendAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model.
func (a *NetHttpRequestAdapter) SendAsync(requestInfo abs.RequestInformation, constructor absser.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) (absser.Parsable, error) {
func (a *NetHttpRequestAdapter) SendAsync(requestInfo *abs.RequestInformation, constructor absser.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) (absser.Parsable, error) {
if requestInfo == nil {
return nil, errors.New("requestInfo cannot be nil")
}
response, err := a.getHttpResponseMessage(requestInfo)
if err != nil {
return nil, err
Expand Down Expand Up @@ -167,7 +170,10 @@ func (a *NetHttpRequestAdapter) SendAsync(requestInfo abs.RequestInformation, co
}

// SendCollectionAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection.
func (a *NetHttpRequestAdapter) SendCollectionAsync(requestInfo abs.RequestInformation, constructor absser.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) ([]absser.Parsable, error) {
func (a *NetHttpRequestAdapter) SendCollectionAsync(requestInfo *abs.RequestInformation, constructor absser.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) ([]absser.Parsable, error) {
if requestInfo == nil {
return nil, errors.New("requestInfo cannot be nil")
}
response, err := a.getHttpResponseMessage(requestInfo)
if err != nil {
return nil, err
Expand Down Expand Up @@ -195,7 +201,10 @@ func (a *NetHttpRequestAdapter) SendCollectionAsync(requestInfo abs.RequestInfor
}

// SendPrimitiveAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model.
func (a *NetHttpRequestAdapter) SendPrimitiveAsync(requestInfo abs.RequestInformation, typeName string, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) (interface{}, error) {
func (a *NetHttpRequestAdapter) SendPrimitiveAsync(requestInfo *abs.RequestInformation, typeName string, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) (interface{}, error) {
if requestInfo == nil {
return nil, errors.New("requestInfo cannot be nil")
}
response, err := a.getHttpResponseMessage(requestInfo)
if err != nil {
return nil, err
Expand Down Expand Up @@ -244,7 +253,10 @@ func (a *NetHttpRequestAdapter) SendPrimitiveAsync(requestInfo abs.RequestInform
}

// SendPrimitiveCollectionAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model collection.
func (a *NetHttpRequestAdapter) SendPrimitiveCollectionAsync(requestInfo abs.RequestInformation, typeName string, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) ([]interface{}, error) {
func (a *NetHttpRequestAdapter) SendPrimitiveCollectionAsync(requestInfo *abs.RequestInformation, typeName string, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) ([]interface{}, error) {
if requestInfo == nil {
return nil, errors.New("requestInfo cannot be nil")
}
response, err := a.getHttpResponseMessage(requestInfo)
if err != nil {
return nil, err
Expand All @@ -271,7 +283,10 @@ func (a *NetHttpRequestAdapter) SendPrimitiveCollectionAsync(requestInfo abs.Req
}

// SendNoContentAsync executes the HTTP request specified by the given RequestInformation with no return content.
func (a *NetHttpRequestAdapter) SendNoContentAsync(requestInfo abs.RequestInformation, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) error {
func (a *NetHttpRequestAdapter) SendNoContentAsync(requestInfo *abs.RequestInformation, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) error {
if requestInfo == nil {
return errors.New("requestInfo cannot be nil")
}
response, err := a.getHttpResponseMessage(requestInfo)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private static void AssignPropertyFromParameter(CodeClass parentClass, CodeMetho
if(property != null) {
var parameter = currentMethod.Parameters.FirstOrDefault(x => x.IsOfKind(parameterKind));
if(!string.IsNullOrEmpty(variableName))
writer.WriteLine($"m.{property.Name.ToFirstCharacterLowerCase()} = {parameter.Name};");
writer.WriteLine($"m.{property.Name.ToFirstCharacterLowerCase()} = {variableName};");
else if(parameter != null)
writer.WriteLine($"m.{property.Name.ToFirstCharacterLowerCase()} = {parameter.Name};");
}
Expand Down Expand Up @@ -393,7 +393,7 @@ _ when string.IsNullOrEmpty(returnType) => "SendNoContentAsync",
var assignmentPrefix = isVoid ?
"err =" :
"res, err :=";
writer.WriteLine($"{assignmentPrefix} m.requestAdapter.{sendMethodName}(*{RequestInfoVarName}, {constructorFunction}{responseHandlerParam?.Name ?? "nil"}, {errorMappingVarName})");
writer.WriteLine($"{assignmentPrefix} m.requestAdapter.{sendMethodName}({RequestInfoVarName}, {constructorFunction}{responseHandlerParam?.Name ?? "nil"}, {errorMappingVarName})");
WriteReturnError(writer, returnType);
var valueVarName = string.Empty;
if(codeElement.ReturnType.CollectionKind != CodeTypeBase.CodeTypeCollectionKind.None) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ public void WritesConstructor() {
var result = tw.ToString();
Assert.Contains(parentClass.Name.ToFirstCharacterUpperCase(), result);
Assert.Contains($"m.{propName} = {defaultValue}", result);
Assert.Contains("m.pathParameters = urlTplParams", result);
Assert.Contains("make(map[string]string)", result);
}
[Fact]
Expand Down

0 comments on commit de55f78

Please sign in to comment.