Skip to content

Commit

Permalink
[JAVA] 4709: codegen with parcelableMode fails to build if using arra…
Browse files Browse the repository at this point in the history
…ys in swagger. (#7867)
  • Loading branch information
SergeyLyakhov authored and wing328 committed Mar 20, 2018
1 parent f943332 commit f023327
Show file tree
Hide file tree
Showing 35 changed files with 314 additions and 274 deletions.
67 changes: 52 additions & 15 deletions modules/swagger-codegen/src/main/resources/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,27 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
{{/isContainer}}

{{/vars}}
{{#parcelableModel}}
public {{classname}}() {
{{#parent}}
super();
{{/parent}}
{{#gson}}
{{#discriminator}}
this.{{discriminator}} = this.getClass().getSimpleName();
{{/discriminator}}
{{/gson}}
}
{{/parcelableModel}}
{{^parcelableModel}}
{{#gson}}
{{#discriminator}}
public {{classname}}() {
this.{{discriminator}} = this.getClass().getSimpleName();
}
{{/discriminator}}
{{/gson}}
{{/parcelableModel}}
{{#vars}}
{{^isReadOnly}}
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
Expand Down Expand Up @@ -203,27 +217,41 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
return o.toString().replace("\n", "\n ");
}

{{#parcelableModel}}
{{#parcelableModel}}

public void writeToParcel(Parcel out, int flags) {
{{#parent}} super.writeToParcel(out, flags); {{/parent}} {{#vars}}
{{#model}}
{{#isArrayModel}}
out.writeList(this);
{{/isArrayModel}}
{{^isArrayModel}}
{{#parent}}
super.writeToParcel(out, flags);
{{/parent}}
{{#vars}}
out.writeValue({{name}});
{{/vars}}
}

public {{classname}}() {
super();
{{/vars}}
{{/isArrayModel}}
{{/model}}
}

{{classname}}(Parcel in) {
{{#parent}} super(in); {{/parent}}
{{#vars}}
{{#isPrimitiveType}}
{{#isArrayModel}}
in.readTypedList(this, {{arrayModelType}}.CREATOR);
{{/isArrayModel}}
{{^isArrayModel}}
{{#parent}}
super(in);
{{/parent}}
{{#vars}}
{{#isPrimitiveType}}
{{name}} = ({{{datatypeWithEnum}}})in.readValue(null);
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{name}} = ({{{datatypeWithEnum}}})in.readValue({{complexType}}.class.getClassLoader());
{{/isPrimitiveType}}
{{/vars}}
{{/isPrimitiveType}}
{{/vars}}
{{/isArrayModel}}
}

public int describeContents() {
Expand All @@ -232,11 +260,20 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela

public static final Parcelable.Creator<{{classname}}> CREATOR = new Parcelable.Creator<{{classname}}>() {
public {{classname}} createFromParcel(Parcel in) {
{{#model}}
{{#isArrayModel}}
{{classname}} result = new {{classname}}();
result.addAll(in.readArrayList({{arrayModelType}}.class.getClassLoader()));
return result;
{{/isArrayModel}}
{{^isArrayModel}}
return new {{classname}}(in);
{{/isArrayModel}}
{{/model}}
}
public {{classname}}[] newArray(int size) {
return new {{classname}}[size];
}
};
{{/parcelableModel}}
{{/parcelableModel}}
}
57 changes: 40 additions & 17 deletions samples/client/petstore/java/okhttp-gson-parcelableModel/README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
# swagger-petstore-okhttp-gson

Swagger Petstore
- API version: 1.0.0

This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\


*Automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen)*


## Requirements

Building the API client library requires [Maven](https://maven.apache.org/) to be installed.
Building the API client library requires:
1. Java 1.7+
2. Maven/Gradle

## Installation

To install the API client library to your local Maven repository, simply execute:

```shell
mvn install
mvn clean install
```

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

```shell
mvn deploy
mvn clean deploy
```

Refer to the [official documentation](https://maven.apache.org/plugins/maven-deploy-plugin/usage.html) for more information.
Refer to the [OSSRH Guide](http://central.sonatype.org/pages/ossrh-guide.html) for more information.

### Maven users

Add this dependency to your project's POM:

```xml
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-petstore-okhttp-gson</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
<groupId>io.swagger</groupId>
<artifactId>swagger-petstore-okhttp-gson</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
```

Expand All @@ -45,12 +56,14 @@ compile "io.swagger:swagger-petstore-okhttp-gson:1.0.0"

At first generate the JAR by executing:

mvn package
```shell
mvn clean package
```

Then manually install the following JARs:

* target/swagger-petstore-okhttp-gson-1.0.0.jar
* target/lib/*.jar
* `target/swagger-petstore-okhttp-gson-1.0.0.jar`
* `target/lib/*.jar`

## Getting Started

Expand All @@ -61,22 +74,22 @@ Please follow the [installation](#installation) instruction and execute the foll
import io.swagger.client.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
import io.swagger.client.api.FakeApi;
import io.swagger.client.api.AnotherFakeApi;

import java.io.File;
import java.util.*;

public class FakeApiExample {
public class AnotherFakeApiExample {

public static void main(String[] args) {

FakeApi apiInstance = new FakeApi();
Boolean body = true; // Boolean | Input boolean as post body
AnotherFakeApi apiInstance = new AnotherFakeApi();
Client body = new Client(); // Client | client model
try {
Boolean result = apiInstance.fakeOuterBooleanSerialize(body);
Client result = apiInstance.testSpecialTags(body);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FakeApi#fakeOuterBooleanSerialize");
System.err.println("Exception when calling AnotherFakeApi#testSpecialTags");
e.printStackTrace();
}
}
Expand All @@ -90,13 +103,17 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*AnotherFakeApi* | [**testSpecialTags**](docs/AnotherFakeApi.md#testSpecialTags) | **PATCH** /another-fake/dummy | To test special tags
*FakeApi* | [**fakeOuterBooleanSerialize**](docs/FakeApi.md#fakeOuterBooleanSerialize) | **POST** /fake/outer/boolean |
*FakeApi* | [**fakeOuterCompositeSerialize**](docs/FakeApi.md#fakeOuterCompositeSerialize) | **POST** /fake/outer/composite |
*FakeApi* | [**fakeOuterNumberSerialize**](docs/FakeApi.md#fakeOuterNumberSerialize) | **POST** /fake/outer/number |
*FakeApi* | [**fakeOuterStringSerialize**](docs/FakeApi.md#fakeOuterStringSerialize) | **POST** /fake/outer/string |
*FakeApi* | [**testClientModel**](docs/FakeApi.md#testClientModel) | **PATCH** /fake | To test \&quot;client\&quot; model
*FakeApi* | [**testEndpointParameters**](docs/FakeApi.md#testEndpointParameters) | **POST** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
*FakeApi* | [**testEnumParameters**](docs/FakeApi.md#testEnumParameters) | **GET** /fake | To test enum parameters
*FakeApi* | [**testInlineAdditionalProperties**](docs/FakeApi.md#testInlineAdditionalProperties) | **POST** /fake/inline-additionalProperties | test inline additionalProperties
*FakeApi* | [**testJsonFormData**](docs/FakeApi.md#testJsonFormData) | **GET** /fake/jsonFormData | test json serialization of form data
*FakeClassnameTags123Api* | [**testClassname**](docs/FakeClassnameTags123Api.md#testClassname) | **PATCH** /fake_classname_test | To test class name in snake case
*PetApi* | [**addPet**](docs/PetApi.md#addPet) | **POST** /pet | Add a new pet to the store
*PetApi* | [**deletePet**](docs/PetApi.md#deletePet) | **DELETE** /pet/{petId} | Deletes a pet
*PetApi* | [**findPetsByStatus**](docs/PetApi.md#findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status
Expand Down Expand Up @@ -164,6 +181,12 @@ Authentication schemes defined for the API:
- **API key parameter name**: api_key
- **Location**: HTTP header

### api_key_query

- **Type**: API key
- **API key parameter name**: api_key_query
- **Location**: URL query string

### http_basic_test

- **Type**: HTTP basic authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**enumString** | [**EnumStringEnum**](#EnumStringEnum) | | [optional]
**enumStringRequired** | [**EnumStringRequiredEnum**](#EnumStringRequiredEnum) | |
**enumInteger** | [**EnumIntegerEnum**](#EnumIntegerEnum) | | [optional]
**enumNumber** | [**EnumNumberEnum**](#EnumNumberEnum) | | [optional]
**outerEnum** | [**OuterEnum**](OuterEnum.md) | | [optional]
Expand All @@ -19,6 +20,15 @@ LOWER | &quot;lower&quot;
EMPTY | &quot;&quot;


<a name="EnumStringRequiredEnum"></a>
## Enum: EnumStringRequiredEnum
Name | Value
---- | -----
UPPER | &quot;UPPER&quot;
LOWER | &quot;lower&quot;
EMPTY | &quot;&quot;


<a name="EnumIntegerEnum"></a>
## Enum: EnumIntegerEnum
Name | Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class AdditionalPropertiesClass implements Parcelable {
@SerializedName("map_of_map_property")
private Map<String, Map<String, String>> mapOfMapProperty = null;

public AdditionalPropertiesClass() {
}
public AdditionalPropertiesClass mapProperty(Map<String, String> mapProperty) {
this.mapProperty = mapProperty;
return this;
Expand Down Expand Up @@ -134,19 +136,13 @@ private String toIndentedString(java.lang.Object o) {
return o.toString().replace("\n", "\n ");
}


public void writeToParcel(Parcel out, int flags) {

out.writeValue(mapProperty);

out.writeValue(mapOfMapProperty);
}

public AdditionalPropertiesClass() {
super();
}

AdditionalPropertiesClass(Parcel in) {

mapProperty = (Map<String, String>)in.readValue(null);
mapOfMapProperty = (Map<String, Map<String, String>>)in.readValue(Map.class.getClassLoader());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,13 @@ private String toIndentedString(java.lang.Object o) {
return o.toString().replace("\n", "\n ");
}


public void writeToParcel(Parcel out, int flags) {

out.writeValue(className);

out.writeValue(color);
}

public Animal() {
super();
}

Animal(Parcel in) {

className = (String)in.readValue(null);
color = (String)in.readValue(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/

public class AnimalFarm extends ArrayList<Animal> implements Parcelable {
public AnimalFarm() {
super();
}

@Override
public boolean equals(java.lang.Object o) {
Expand Down Expand Up @@ -64,15 +67,13 @@ private String toIndentedString(java.lang.Object o) {
return o.toString().replace("\n", "\n ");
}

public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags); }

public AnimalFarm() {
super();
public void writeToParcel(Parcel out, int flags) {
out.writeList(this);
}

AnimalFarm(Parcel in) {
super(in);
in.readTypedList(this, Animal.CREATOR);
}

public int describeContents() {
Expand All @@ -81,7 +82,9 @@ public int describeContents() {

public static final Parcelable.Creator<AnimalFarm> CREATOR = new Parcelable.Creator<AnimalFarm>() {
public AnimalFarm createFromParcel(Parcel in) {
return new AnimalFarm(in);
AnimalFarm result = new AnimalFarm();
result.addAll(in.readArrayList(Animal.class.getClassLoader()));
return result;
}
public AnimalFarm[] newArray(int size) {
return new AnimalFarm[size];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ArrayOfArrayOfNumberOnly implements Parcelable {
@SerializedName("ArrayArrayNumber")
private List<List<BigDecimal>> arrayArrayNumber = null;

public ArrayOfArrayOfNumberOnly() {
}
public ArrayOfArrayOfNumberOnly arrayArrayNumber(List<List<BigDecimal>> arrayArrayNumber) {
this.arrayArrayNumber = arrayArrayNumber;
return this;
Expand Down Expand Up @@ -103,17 +105,12 @@ private String toIndentedString(java.lang.Object o) {
return o.toString().replace("\n", "\n ");
}


public void writeToParcel(Parcel out, int flags) {

out.writeValue(arrayArrayNumber);
}

public ArrayOfArrayOfNumberOnly() {
super();
}

ArrayOfArrayOfNumberOnly(Parcel in) {

arrayArrayNumber = (List<List<BigDecimal>>)in.readValue(List.class.getClassLoader());
}

Expand Down
Loading

0 comments on commit f023327

Please sign in to comment.