forked from loafle/openapi-generator-original
Add alias type definitions for Java
When a spec defines a Model at the top level that is a non-aggretate type (such
as string, number or boolean), it essentially represents an alias for the simple
type. For example, the following spec snippet creates an alias of the boolean
type that for all intents and purposes acts just like a regular boolean.
definitions:
JustABoolean:
type: boolean
This can be modeled in some languages through built-in mechanisms, such as
typedefs in C++. Java, however, just not have a clean way of representing this.
This change introduces an internal mechanism for representing aliases. It
maintains a map in DefaultCodegen that tracks these types of definitions, and
wherever it sees the "JustABoolean" type in the spec, it generates code that
uses the built-in "Boolean" instead.
This functionality currenlty only applies to Java, but could be extended to
other languages later.
The change adds a few examples of this to the fake endpoint spec for testing,
which means all of the samples change as well.
This commit is contained in:
@@ -1,9 +1,21 @@
|
||||
package io.swagger.client.api;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.swagger.client.ApiClient;
|
||||
import io.swagger.client.model.OuterComposite;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import okhttp3.mockwebserver.RecordedRequest;
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -18,12 +30,31 @@ import java.util.Map;
|
||||
public class FakeApiTest {
|
||||
|
||||
private FakeApi api;
|
||||
private MockWebServer mockServer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
api = new ApiClient().buildClient(FakeApi.class);
|
||||
public void setup() throws IOException {
|
||||
mockServer = new MockWebServer();
|
||||
mockServer.start();
|
||||
api = new ApiClient().setBasePath(mockServer.url("/").toString()).buildClient(FakeApi.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws IOException {
|
||||
mockServer.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the HTTP request body from the mock server as a String.
|
||||
* @param request The mock server's request.
|
||||
* @return A String representation of the body of the request.
|
||||
* @throws IOException On error reading the body of the request.
|
||||
*/
|
||||
private static String requestBody(RecordedRequest request) throws IOException {
|
||||
ByteArrayOutputStream body = new ByteArrayOutputStream((int) request.getBodySize());
|
||||
request.getBody().copyTo(body);
|
||||
return body.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
|
||||
@@ -48,5 +79,52 @@ public class FakeApiTest {
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testOuterNumber() throws Exception {
|
||||
mockServer.enqueue(new MockResponse().setBody("5"));
|
||||
BigDecimal response = api.fakeOuterNumberSerialize(new BigDecimal(3));
|
||||
assertThat(requestBody(mockServer.takeRequest())).isEqualTo("3");
|
||||
assertThat(response).isEqualTo(new BigDecimal(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOuterString() throws Exception {
|
||||
mockServer.enqueue(new MockResponse().setBody("\"Hello from the server\""));
|
||||
String response = api.fakeOuterStringSerialize("Hello from the client");
|
||||
assertThat(requestBody(mockServer.takeRequest())).isEqualTo("\"Hello from the client\"");
|
||||
assertThat(response).isEqualTo("Hello from the server");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOuterBoolean() throws Exception {
|
||||
mockServer.enqueue(new MockResponse().setBody("true"));
|
||||
Boolean response = api.fakeOuterBooleanSerialize(false);
|
||||
assertThat(requestBody(mockServer.takeRequest())).isEqualTo("false");
|
||||
assertThat(response).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOuterComposite() throws Exception {
|
||||
mockServer.enqueue(new MockResponse().setBody(
|
||||
"{\"my_number\": 5, \"my_string\": \"Hello from the server\", \"my_boolean\": true}"));
|
||||
OuterComposite compositeRequest = new OuterComposite()
|
||||
.myNumber(new BigDecimal(3))
|
||||
.myString("Hello from the client")
|
||||
.myBoolean(false);
|
||||
OuterComposite response = api.fakeOuterCompositeSerialize(compositeRequest);
|
||||
|
||||
JsonNode requestJson = new ObjectMapper()
|
||||
.readValue(requestBody(mockServer.takeRequest()), JsonNode.class);
|
||||
assertThat(requestJson.fieldNames()).contains("my_number", "my_string", "my_boolean");
|
||||
assertThat(requestJson.get("my_number").intValue()).isEqualTo(3);
|
||||
assertThat(requestJson.get("my_string").textValue()).isEqualTo("Hello from the client");
|
||||
assertThat(requestJson.get("my_boolean").booleanValue()).isEqualTo(false);
|
||||
assertThat(response).isEqualTo(
|
||||
new OuterComposite()
|
||||
.myNumber(new BigDecimal(5))
|
||||
.myString("Hello from the server")
|
||||
.myBoolean(true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user