Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Java client, adds responses class + deserializer #394

Merged
merged 18 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion samples/client/3_0_3_unit_test/java/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/UriTemplateFo
src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java
src/main/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlags.java
src/main/java/org/openapijsonschematools/client/configurations/SchemaConfiguration.java
src/main/java/org/openapijsonschematools/client/exceptions/ApiException.java
src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java
src/main/java/org/openapijsonschematools/client/exceptions/InvalidAdditionalPropertyException.java
src/main/java/org/openapijsonschematools/client/exceptions/InvalidTypeException.java
Expand All @@ -191,8 +192,9 @@ src/main/java/org/openapijsonschematools/client/requestbody/GenericRequestBody.j
src/main/java/org/openapijsonschematools/client/requestbody/RequestBodySerializer.java
src/main/java/org/openapijsonschematools/client/requestbody/SerializedRequestBody.java
src/main/java/org/openapijsonschematools/client/response/ApiResponse.java
src/main/java/org/openapijsonschematools/client/response/DeserializedApiResponse.java
src/main/java/org/openapijsonschematools/client/response/DeserializedHttpResponse.java
src/main/java/org/openapijsonschematools/client/response/ResponseDeserializer.java
src/main/java/org/openapijsonschematools/client/response/ResponsesDeserializer.java
src/main/java/org/openapijsonschematools/client/schemas/AnyTypeJsonSchema.java
src/main/java/org/openapijsonschematools/client/schemas/BooleanJsonSchema.java
src/main/java/org/openapijsonschematools/client/schemas/DateJsonSchema.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openapijsonschematools.client.exceptions;

import java.net.http.HttpResponse;

@SuppressWarnings("serial")
public class ApiException extends BaseException {
public HttpResponse<byte[]> response;

public ApiException(String s, HttpResponse<byte[]> response) {
super(s);
this.response = response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

@SuppressWarnings("serial")
public class BaseException extends RuntimeException {
public BaseException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class InvalidAdditionalPropertyException extends BaseException {
public InvalidAdditionalPropertyException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class InvalidTypeException extends BaseException {
public InvalidTypeException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class UnsetPropertyException extends BaseException {
public UnsetPropertyException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class ValidationException extends BaseException {
public ValidationException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.openapijsonschematools.client.response;

import java.net.http.HttpResponse;

public record DeserializedHttpResponse<SealedBodyOutputClass, HeaderOutputClass>(SealedBodyOutputClass body, HeaderOutputClass headers) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected <T> T deserializeBody(String contentType, byte[] body, JsonSchema<T> s
throw new RuntimeException("Deserialization for contentType="+contentType+" has not yet been implemented.");
}

public ApiResponse<SealedBodyClass, HeaderClass> deserialize(HttpResponse<byte[]> response, SchemaConfiguration configuration) {
public DeserializedHttpResponse<SealedBodyClass, HeaderClass> deserialize(HttpResponse<byte[]> response, SchemaConfiguration configuration) {
Optional<String> contentTypeInfo = response.headers().firstValue("Content-Type");
if (contentTypeInfo.isEmpty()) {
throw new RuntimeException("Invalid response returned, Content-Type header is missing and it must be included");
Expand All @@ -78,6 +78,6 @@ public ApiResponse<SealedBodyClass, HeaderClass> deserialize(HttpResponse<byte[]
byte[] bodyBytes = response.body();
SealedBodyClass body = getBody(contentType, bodyBytes, configuration);
HeaderClass headers = getHeaders(response.headers());
return new DeserializedApiResponse<>(response, body, headers);
return new DeserializedHttpResponse<>(body, headers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openapijsonschematools.client.response;

import org.openapijsonschematools.client.configurations.SchemaConfiguration;
import java.net.http.HttpResponse;

public interface ResponsesDeserializer<SealedResponseClass> {
SealedResponseClass deserialize(HttpResponse<byte[]> response, SchemaConfiguration configuration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ public void testDeserializeApplicationJsonNull() {
byte[] bodyBytes = toJson(null).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -174,8 +173,7 @@ public void testDeserializeApplicationJsonTrue() {
byte[] bodyBytes = toJson(true).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -191,8 +189,7 @@ public void testDeserializeApplicationJsonFalse() {
byte[] bodyBytes = toJson(false).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -208,8 +205,7 @@ public void testDeserializeApplicationJsonInt() {
byte[] bodyBytes = toJson(1).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -225,8 +221,7 @@ public void testDeserializeApplicationJsonFloat() {
byte[] bodyBytes = toJson(3.14).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -242,8 +237,7 @@ public void testDeserializeApplicationJsonString() {
byte[] bodyBytes = toJson("a").getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand Down
4 changes: 3 additions & 1 deletion samples/client/3_1_0_unit_test/java/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ src/main/java/org/openapijsonschematools/client/components/schemas/ValidateAgain
src/main/java/org/openapijsonschematools/client/configurations/ApiConfiguration.java
src/main/java/org/openapijsonschematools/client/configurations/JsonSchemaKeywordFlags.java
src/main/java/org/openapijsonschematools/client/configurations/SchemaConfiguration.java
src/main/java/org/openapijsonschematools/client/exceptions/ApiException.java
src/main/java/org/openapijsonschematools/client/exceptions/BaseException.java
src/main/java/org/openapijsonschematools/client/exceptions/InvalidAdditionalPropertyException.java
src/main/java/org/openapijsonschematools/client/exceptions/InvalidTypeException.java
Expand All @@ -303,8 +304,9 @@ src/main/java/org/openapijsonschematools/client/requestbody/GenericRequestBody.j
src/main/java/org/openapijsonschematools/client/requestbody/RequestBodySerializer.java
src/main/java/org/openapijsonschematools/client/requestbody/SerializedRequestBody.java
src/main/java/org/openapijsonschematools/client/response/ApiResponse.java
src/main/java/org/openapijsonschematools/client/response/DeserializedApiResponse.java
src/main/java/org/openapijsonschematools/client/response/DeserializedHttpResponse.java
src/main/java/org/openapijsonschematools/client/response/ResponseDeserializer.java
src/main/java/org/openapijsonschematools/client/response/ResponsesDeserializer.java
src/main/java/org/openapijsonschematools/client/schemas/AnyTypeJsonSchema.java
src/main/java/org/openapijsonschematools/client/schemas/BooleanJsonSchema.java
src/main/java/org/openapijsonschematools/client/schemas/DateJsonSchema.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openapijsonschematools.client.exceptions;

import java.net.http.HttpResponse;

@SuppressWarnings("serial")
public class ApiException extends BaseException {
public HttpResponse<byte[]> response;

public ApiException(String s, HttpResponse<byte[]> response) {
super(s);
this.response = response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

@SuppressWarnings("serial")
public class BaseException extends RuntimeException {
public BaseException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class InvalidAdditionalPropertyException extends BaseException {
public InvalidAdditionalPropertyException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class InvalidTypeException extends BaseException {
public InvalidTypeException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class UnsetPropertyException extends BaseException {
public UnsetPropertyException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@SuppressWarnings("serial")
public class ValidationException extends BaseException {
public ValidationException(String s) {
super();
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.openapijsonschematools.client.response;

import java.net.http.HttpResponse;

public record DeserializedHttpResponse<SealedBodyOutputClass, HeaderOutputClass>(SealedBodyOutputClass body, HeaderOutputClass headers) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected <T> T deserializeBody(String contentType, byte[] body, JsonSchema<T> s
throw new RuntimeException("Deserialization for contentType="+contentType+" has not yet been implemented.");
}

public ApiResponse<SealedBodyClass, HeaderClass> deserialize(HttpResponse<byte[]> response, SchemaConfiguration configuration) {
public DeserializedHttpResponse<SealedBodyClass, HeaderClass> deserialize(HttpResponse<byte[]> response, SchemaConfiguration configuration) {
Optional<String> contentTypeInfo = response.headers().firstValue("Content-Type");
if (contentTypeInfo.isEmpty()) {
throw new RuntimeException("Invalid response returned, Content-Type header is missing and it must be included");
Expand All @@ -78,6 +78,6 @@ public ApiResponse<SealedBodyClass, HeaderClass> deserialize(HttpResponse<byte[]
byte[] bodyBytes = response.body();
SealedBodyClass body = getBody(contentType, bodyBytes, configuration);
HeaderClass headers = getHeaders(response.headers());
return new DeserializedApiResponse<>(response, body, headers);
return new DeserializedHttpResponse<>(body, headers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openapijsonschematools.client.response;

import org.openapijsonschematools.client.configurations.SchemaConfiguration;
import java.net.http.HttpResponse;

public interface ResponsesDeserializer<SealedResponseClass> {
SealedResponseClass deserialize(HttpResponse<byte[]> response, SchemaConfiguration configuration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ public void testDeserializeApplicationJsonNull() {
byte[] bodyBytes = toJson(null).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -174,8 +173,7 @@ public void testDeserializeApplicationJsonTrue() {
byte[] bodyBytes = toJson(true).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -191,8 +189,7 @@ public void testDeserializeApplicationJsonFalse() {
byte[] bodyBytes = toJson(false).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -208,8 +205,7 @@ public void testDeserializeApplicationJsonInt() {
byte[] bodyBytes = toJson(1).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -225,8 +221,7 @@ public void testDeserializeApplicationJsonFloat() {
byte[] bodyBytes = toJson(3.14).getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand All @@ -242,8 +237,7 @@ public void testDeserializeApplicationJsonString() {
byte[] bodyBytes = toJson("a").getBytes(StandardCharsets.UTF_8);
BytesHttpResponse response = new BytesHttpResponse(bodyBytes, "application/json");
SchemaConfiguration configuration = new SchemaConfiguration(JsonSchemaKeywordFlags.ofNone());
ApiResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
Assert.assertEquals(response, apiResponse.response());
DeserializedHttpResponse<SealedResponseBody, Void> apiResponse = deserializer.deserialize(response, configuration);
if (!(apiResponse.body() instanceof ApplicationjsonBody jsonBody)) {
throw new RuntimeException("body must be type ApplicationjsonBody");
}
Expand Down
Loading