public void testAutoCreateWithDisabledDynamicMappings() throws Exception {
    assertAcked(
        client()
            .admin()
            .indices()
            .preparePutTemplate("my_template")
            .setCreate(true)
            .setPatterns(Collections.singletonList("index_*"))
            .addMapping("foo", "field", "type=keyword")
            .setSettings(Settings.builder().put("index.mapper.dynamic", false).build())
            .get());

    // succeeds since 'foo' has an explicit mapping in the template
    indexRandom(
        true, false, client().prepareIndex("index_1", "foo", "1").setSource("field", "abc"));

    // fails since 'bar' does not have an explicit mapping in the template and dynamic template
    // creation is disabled
    TypeMissingException e1 =
        expectThrows(
            TypeMissingException.class,
            () -> client().prepareIndex("index_2", "bar", "1").setSource("field", "abc").get());
    assertEquals("type[bar] missing", e1.getMessage());
    assertEquals(
        "trying to auto create mapping, but dynamic mapping is disabled",
        e1.getCause().getMessage());

    // make sure no mappings were created for bar
    GetIndexResponse getIndexResponse =
        client().admin().indices().prepareGetIndex().addIndices("index_2").get();
    assertFalse(getIndexResponse.mappings().containsKey("bar"));
  }