Skip to content

Commit

Permalink
#11727: Fixed NPE on empty required-fields provided to (#11743) (#11744)
Browse files Browse the repository at this point in the history
field-resource rest-api
  • Loading branch information
agomez-dotcms authored and dsilvam committed May 26, 2017
1 parent 27e7a77 commit 44e6c6c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.dotcms.contenttype.model.component.FieldValueRenderer;
import com.dotcms.repackage.com.google.common.base.Preconditions;
import com.dotcms.repackage.com.google.common.collect.ImmutableList;
import com.dotcms.repackage.org.apache.commons.lang.StringUtils;
import com.dotcms.repackage.org.apache.commons.lang.time.DateUtils;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.FactoryLocator;
Expand Down Expand Up @@ -62,6 +63,8 @@ public abstract class Field implements FieldIf, Serializable {

@Value.Check
public void check() {
Preconditions.checkArgument(StringUtils.isNotEmpty(name()), "Name cannot be empty for " + this.getClass());

/*if (iDate().after(legacyFieldDate)) {
Preconditions.checkArgument(acceptedDataTypes().contains(dataType()),
this.getClass().getSimpleName() + " must have DataType:" + acceptedDataTypes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ public JsonFieldTransformer(String json) {
List<Field> l = new ArrayList<>();
// are we an array?
try {
JSONObject jo = new JSONObject(json);
if (jo.has("fields")) {
l = fromJsonArray(jo.getJSONArray("fields"));
} else {
l.add(fromJsonStr(json));
JSONArray jarr = new JSONArray(json);
if (jarr.size() > 0) {
JSONObject jo = jarr.getJSONObject(0);
if (jo.has("fields")) {
l = fromJsonArray(jo.getJSONArray("fields"));
} else {
l = fromJsonArrayStr(json);
}
}
} catch (Exception e) {
try {
JSONArray jarr = new JSONArray(json);
if (jarr.size() > 0) {
JSONObject jo = jarr.getJSONObject(0);
if (jo.has("fields")) {
l = fromJsonArray(jo.getJSONArray("fields"));
} else {
l = fromJsonArrayStr(json);
}
JSONObject jo = new JSONObject(json);
if (jo.has("fields")) {
l = fromJsonArray(jo.getJSONArray("fields"));
} else {
l.add(fromJsonStr(json));
}
} catch (Exception ex) {
throw new DotStateException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.dotcms.rest.annotation.NoCache;
import com.dotcms.rest.exception.mapper.ExceptionMapperUtil;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.UtilMethods;
Expand Down Expand Up @@ -76,6 +77,10 @@ public Response createContentTypeField(@PathParam("typeId") final String typeId,

response = Response.ok(new ResponseEntityView(new JsonFieldTransformer(field).mapObject())).build();
}
} catch (DotStateException e) {

response = ExceptionMapperUtil.createResponse(null, "Field is not valid ("+ e.getMessage() +")");

} catch (NotFoundInDbException e) {

response = ExceptionMapperUtil.createResponse(e, Response.Status.NOT_FOUND);
Expand Down Expand Up @@ -220,6 +225,10 @@ public Response updateContentTypeFieldById(@PathParam("typeId") final String typ
response = Response.ok(new ResponseEntityView(new JsonFieldTransformer(field).mapObject())).build();
}
}
} catch (DotStateException e) {

response = ExceptionMapperUtil.createResponse(null, "Field is not valid ("+ e.getMessage() +")");

} catch (NotFoundInDbException e) {

response = ExceptionMapperUtil.createResponse(e, Response.Status.NOT_FOUND);
Expand Down Expand Up @@ -268,6 +277,10 @@ public Response updateContentTypeFieldByVar(@PathParam("typeId") final String ty
response = Response.ok(new ResponseEntityView(new JsonFieldTransformer(field).mapObject())).build();
}
}
} catch (DotStateException e) {

response = ExceptionMapperUtil.createResponse(null, "Field is not valid ("+ e.getMessage() +")");

} catch (NotFoundInDbException e) {

response = ExceptionMapperUtil.createResponse(e, Response.Status.NOT_FOUND);
Expand Down

0 comments on commit 44e6c6c

Please sign in to comment.