Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Constructor<?> ctor = getDefaultCtor(rawType);
if (ctor == null) {
LOGGER.warning("Instance of class " + rawType.getName() + " is a subclass of DynamicModel, but it doesn't "
+ "have a public default constructor. This instance will be ignored by " + this.getClass().getSimpleName());
+ "define a default constructor. This instance will be ignored by " + this.getClass().getSimpleName());
return null;
}

Expand All @@ -121,10 +121,13 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
* @return clazz's default ctor
*/
protected Constructor<?> getDefaultCtor(Class<?> clazz) {
Constructor<?>[] allCtors = clazz.getConstructors();
Constructor<?>[] allCtors = clazz.getDeclaredConstructors();
for (int i = 0; i < allCtors.length; i++) {
Constructor<?> ctor = allCtors[i];
if (ctor.getParameterTypes().length == 0) {
if (!ctor.isAccessible()) {
accessor.makeAccessible(ctor);
}
return ctor;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.ibm.cloud.sdk.core.test.model.generated.ModelAPFooNullTypeToken;
import com.ibm.cloud.sdk.core.test.model.generated.ModelAPInteger;
import com.ibm.cloud.sdk.core.test.model.generated.ModelAPObject;
import com.ibm.cloud.sdk.core.test.model.generated.ModelAPProtectedCtor;
import com.ibm.cloud.sdk.core.test.model.generated.ModelAPString;
import com.ibm.cloud.sdk.core.util.GsonSingleton;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -130,6 +131,15 @@ private ModelAPString createModelAPString() {
return model;
}

private ModelAPProtectedCtor createModelAPProtectedCtor() {
ModelAPProtectedCtor model = new ModelAPProtectedCtor("x");
model.setProp1("value1");
model.setProp2(Long.valueOf(33));
model.put("baseball", "C");
model.put("football", "LT");
return model;
}

private Foo createFoo(String foo, int bar) {
Foo fooModel = new Foo();
fooModel.setFoo(foo);
Expand Down Expand Up @@ -261,6 +271,13 @@ public void testModelAPFooNull() {
assertNull(model);
}

@Test
public void testModelAPProtectedCtor() {
ModelAPProtectedCtor model = createModelAPProtectedCtor();
// model.put("basketball", Integer.valueOf(33));
testSerDeser(model, ModelAPProtectedCtor.class, false);
}

@Test(expectedExceptions = {JsonSyntaxException.class}, expectedExceptionsMessageRegExp="Duplicate key: baseball")
public void testModelAPFooDuplicateKey() {
// Serialize a ModelAPFoo instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public class ModelAPFooNoCtor extends DynamicModel<Foo> {
@SerializedName(value="prop2", alternate={"property2", "p2"})
private Long prop2;

private ModelAPFooNoCtor() {
super(null);
}

public ModelAPFooNoCtor(String x) {
super(new TypeToken<Foo>(){});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2018 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.ibm.cloud.sdk.core.test.model.generated;

import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.ibm.cloud.sdk.core.service.model.DynamicModel;

/**
* Model with additionalProperties set to a string schema.
*/
public class ModelAPProtectedCtor extends DynamicModel<String> {
@SerializedName("prop1")
private String prop1;
@SerializedName("prop2")
private Long prop2;

protected ModelAPProtectedCtor() {
super(new TypeToken<String>(){});
}

public ModelAPProtectedCtor(String x) {
this();
}

/**
* Gets the prop1.
*
* String property.
*
* @return the prop1
*/
public String prop1() {
return this.prop1;
}

/**
* Sets the prop1.
*
* @param prop1 the new prop1
*/
public void setProp1(final String prop1) {
this.prop1 = prop1;
}

/**
* Gets the prop2.
*
* Integer property.
*
* @return the prop2
*/
public Long prop2() {
return this.prop2;
}

/**
* Sets the prop2.
*
* @param prop2 the new prop2
*/
public void setProp2(final long prop2) {
this.prop2 = prop2;
}
}