Skip to content

Commit

Permalink
NXP-30052: do not allow unexisting document type
Browse files Browse the repository at this point in the history
  • Loading branch information
troger committed Mar 24, 2021
1 parent 9a888e0 commit 30e79c4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package org.nuxeo.ecm.core.api.impl;

import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static org.nuxeo.ecm.core.schema.types.ComplexTypeImpl.canonicalXPath;

import java.io.Serializable;
Expand All @@ -41,6 +42,7 @@
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.LifeCycleConstants;
import org.nuxeo.ecm.core.api.Lock;
import org.nuxeo.ecm.core.api.NuxeoException;
import org.nuxeo.ecm.core.api.NuxeoPrincipal;
import org.nuxeo.ecm.core.api.PathRef;
import org.nuxeo.ecm.core.api.PropertyException;
Expand Down Expand Up @@ -115,6 +117,9 @@ protected SimpleDocumentModel(DocumentType documentType) {
public static SimpleDocumentModel ofType(String type) {
SchemaManager service = Framework.getService(SchemaManager.class);
DocumentType dType = service.getDocumentType(type);
if (dType == null) {
throw new NuxeoException("Type: " + type + " does not exist", SC_BAD_REQUEST);
}
return new SimpleDocumentModel(dType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package org.nuxeo.ecm.core.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -115,4 +117,17 @@ public void testPropertyUpdatedWithSameValueAreDirty7() throws Exception {
assertTrue(doc.getProperty("dc:title").isDirty());
}

// NXP-30052
@Test
public void testNonExistingType() {
try {
SimpleDocumentModel.ofType("Foo");
fail("Should have raised a NuxeoException");
} catch (NuxeoException e) {
assertEquals(400, e.getStatusCode());
assertEquals("Type: Foo does not exist", e.getMessage());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,22 @@ public void iCanCreateADocumentWithNonExistingField() {
}
}

// NXP-30052
@Test
public void iCantCreateADocumentWithNonExistingType() throws IOException {
DocumentModel folder = RestServerInit.getFolder(0, session);

String data = "{\"entity-type\": \"document\",\"type\": \"Foo\",\"name\":\"newName\",\"properties\": {\"dc:title\":\"Foo\"}}";

try (CloseableClientResponse response = getResponse(RequestType.POST, "path" + folder.getPathAsString(),
data)) {
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
JsonNode node = mapper.readTree(response.getEntityInputStream());
assertEquals(400, node.get("status").longValue());
assertEquals("Type: Foo does not exist", node.get("message").textValue());
}
}

@Test
public void iCanDeleteADocument() {
// Given a document
Expand Down

0 comments on commit 30e79c4

Please sign in to comment.