Skip to content

Commit

Permalink
Merge pull request #243 from andreaTP/fixEmptyBodyPOST
Browse files Browse the repository at this point in the history
fix: method POST must have a request body
  • Loading branch information
baywet authored Mar 16, 2023
2 parents 77e2259 + ff0731f commit 1d9ca7a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

### Changed

## [0.3.2] - 2023-03-16

### Added

### Changed

- Fix add an empty body when method is POST/PUT/PATCH and the body is null

## [0.3.1] - 2023-03-08

# Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.microsoft.kiota.ApiClientBuilder;
import com.microsoft.kiota.ApiException;
import com.microsoft.kiota.HttpMethod;
import com.microsoft.kiota.RequestInformation;
import com.microsoft.kiota.RequestOption;
import com.microsoft.kiota.ResponseHandlerOption;
Expand Down Expand Up @@ -766,7 +767,7 @@ public <T> CompletableFuture<T> convertToNativeRequestAsync(@Nonnull final Reque
span.end();
}
}
private Request getRequestFromRequestInformation(@Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes) throws URISyntaxException, MalformedURLException {
protected @Nonnull Request getRequestFromRequestInformation(@Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes) throws URISyntaxException, MalformedURLException {
final Span span = GlobalOpenTelemetry.getTracer(obsOptions.getTracerInstrumentationName()).spanBuilder("getRequestFromRequestInformation").setParent(Context.current().with(parentSpan)).startSpan();
try(final Scope scope = span.makeCurrent()) {
spanForAttributes.setAttribute(SemanticAttributes.HTTP_METHOD, requestInfo.httpMethod.toString());
Expand All @@ -778,8 +779,7 @@ private Request getRequestFromRequestInformation(@Nonnull final RequestInformati
spanForAttributes.setAttribute(SemanticAttributes.HTTP_HOST, requestURL.getHost());
spanForAttributes.setAttribute(SemanticAttributes.HTTP_SCHEME, requestURL.getProtocol());


final RequestBody body = requestInfo.content == null ?
RequestBody body = requestInfo.content == null ?
null :
new RequestBody() {
@Override
Expand All @@ -800,6 +800,14 @@ public void writeTo(BufferedSink sink) throws IOException {
}

};

// https://stackoverflow.com/a/35743536
if (body == null &&
(requestInfo.httpMethod.equals(HttpMethod.POST) ||
requestInfo.httpMethod.equals(HttpMethod.PATCH) ||
requestInfo.httpMethod.equals(HttpMethod.PUT))) {
body = RequestBody.create(new byte[0]);
}
final Request.Builder requestBuilder = new Request.Builder()
.url(requestURL)
.method(requestInfo.httpMethod.toString(), body);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.microsoft.kiota.http;

import static org.junit.jupiter.api.Assertions.*;
import io.opentelemetry.api.GlobalOpenTelemetry;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;

import com.microsoft.kiota.authentication.AuthenticationProvider;
Expand Down Expand Up @@ -33,6 +35,24 @@
import okhttp3.ResponseBody;

public class OkHttpRequestAdapterTest {
@ParameterizedTest
@EnumSource(value = HttpMethod.class, names = {"PUT", "POST", "PATCH"})
public void PostRequestsShouldHaveEmptyBody(HttpMethod method) throws Exception { // Unexpected exception thrown: java.lang.IllegalArgumentException: method POST must have a request body.
final var authenticationProviderMock = mock(AuthenticationProvider.class);
final var adapter = new OkHttpRequestAdapter(authenticationProviderMock) {
public Request test() throws Exception {
var ri = new RequestInformation();
ri.httpMethod = method;
ri.urlTemplate = "http://localhost:1234";
var span1 = GlobalOpenTelemetry.getTracer("").spanBuilder("").startSpan();
var span2 = GlobalOpenTelemetry.getTracer("").spanBuilder("").startSpan();
return this.getRequestFromRequestInformation(ri, span1, span2);
}
};

final var request = assertDoesNotThrow(() -> adapter.test());
assertNotNull(request.body());
}
@ParameterizedTest
@ValueSource(ints = {200, 201, 202, 203, 206})
public void SendStreamReturnsUsableStream(int statusCode) throws Exception {
Expand Down

0 comments on commit 1d9ca7a

Please sign in to comment.