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:
@@ -7,6 +7,7 @@ import java.math.BigDecimal;
|
||||
import io.swagger.client.model.Client;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import io.swagger.client.model.OuterComposite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -18,6 +19,58 @@ import feign.*;
|
||||
public interface FakeApi extends ApiClient.Api {
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Test serialization of outer boolean types
|
||||
* @param body Input boolean as post body (optional)
|
||||
* @return Boolean
|
||||
*/
|
||||
@RequestLine("POST /fake/outer/boolean")
|
||||
@Headers({
|
||||
"Content-Type: application/json",
|
||||
"Accept: application/json",
|
||||
})
|
||||
Boolean fakeOuterBooleanSerialize(Boolean body);
|
||||
|
||||
/**
|
||||
*
|
||||
* Test serialization of object with outer number type
|
||||
* @param body Input composite as post body (optional)
|
||||
* @return OuterComposite
|
||||
*/
|
||||
@RequestLine("POST /fake/outer/composite")
|
||||
@Headers({
|
||||
"Content-Type: application/json",
|
||||
"Accept: application/json",
|
||||
})
|
||||
OuterComposite fakeOuterCompositeSerialize(OuterComposite body);
|
||||
|
||||
/**
|
||||
*
|
||||
* Test serialization of outer number types
|
||||
* @param body Input number as post body (optional)
|
||||
* @return BigDecimal
|
||||
*/
|
||||
@RequestLine("POST /fake/outer/number")
|
||||
@Headers({
|
||||
"Content-Type: application/json",
|
||||
"Accept: application/json",
|
||||
})
|
||||
BigDecimal fakeOuterNumberSerialize(BigDecimal body);
|
||||
|
||||
/**
|
||||
*
|
||||
* Test serialization of outer string types
|
||||
* @param body Input string as post body (optional)
|
||||
* @return String
|
||||
*/
|
||||
@RequestLine("POST /fake/outer/string")
|
||||
@Headers({
|
||||
"Content-Type: application/json",
|
||||
"Accept: application/json",
|
||||
})
|
||||
String fakeOuterStringSerialize(String body);
|
||||
|
||||
/**
|
||||
* To test \"client\" model
|
||||
* To test \"client\" model
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Swagger Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
*
|
||||
* OpenAPI spec version: 1.0.0
|
||||
* Contact: apiteam@swagger.io
|
||||
*
|
||||
* NOTE: This class is auto generated by the swagger code generator program.
|
||||
* https://github.com/swagger-api/swagger-codegen.git
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
|
||||
package io.swagger.client.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* OuterComposite
|
||||
*/
|
||||
|
||||
public class OuterComposite {
|
||||
@JsonProperty("my_number")
|
||||
private BigDecimal myNumber = null;
|
||||
|
||||
@JsonProperty("my_string")
|
||||
private String myString = null;
|
||||
|
||||
@JsonProperty("my_boolean")
|
||||
private Boolean myBoolean = null;
|
||||
|
||||
public OuterComposite myNumber(BigDecimal myNumber) {
|
||||
this.myNumber = myNumber;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get myNumber
|
||||
* @return myNumber
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
public BigDecimal getMyNumber() {
|
||||
return myNumber;
|
||||
}
|
||||
|
||||
public void setMyNumber(BigDecimal myNumber) {
|
||||
this.myNumber = myNumber;
|
||||
}
|
||||
|
||||
public OuterComposite myString(String myString) {
|
||||
this.myString = myString;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get myString
|
||||
* @return myString
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
public String getMyString() {
|
||||
return myString;
|
||||
}
|
||||
|
||||
public void setMyString(String myString) {
|
||||
this.myString = myString;
|
||||
}
|
||||
|
||||
public OuterComposite myBoolean(Boolean myBoolean) {
|
||||
this.myBoolean = myBoolean;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get myBoolean
|
||||
* @return myBoolean
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
public Boolean getMyBoolean() {
|
||||
return myBoolean;
|
||||
}
|
||||
|
||||
public void setMyBoolean(Boolean myBoolean) {
|
||||
this.myBoolean = myBoolean;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(java.lang.Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
OuterComposite outerComposite = (OuterComposite) o;
|
||||
return Objects.equals(this.myNumber, outerComposite.myNumber) &&
|
||||
Objects.equals(this.myString, outerComposite.myString) &&
|
||||
Objects.equals(this.myBoolean, outerComposite.myBoolean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(myNumber, myString, myBoolean);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("class OuterComposite {\n");
|
||||
|
||||
sb.append(" myNumber: ").append(toIndentedString(myNumber)).append("\n");
|
||||
sb.append(" myString: ").append(toIndentedString(myString)).append("\n");
|
||||
sb.append(" myBoolean: ").append(toIndentedString(myBoolean)).append("\n");
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given object to string with each line indented by 4 spaces
|
||||
* (except the first line).
|
||||
*/
|
||||
private String toIndentedString(java.lang.Object o) {
|
||||
if (o == null) {
|
||||
return "null";
|
||||
}
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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