title: vertx class cast exception
body: <p>I'm doing a POST specified in the Swagger as follow:</p>
  
  <pre><code>    post:
        tags:
          - entries
        summary: Add an entry to the phonebook
        operationId: addEntry
        produces:
          - application/json
        consumes:
          - application/json
        parameters:
         - in: body
           name: newEntry
           schema:
            $ref: "#/definitions/Entry"
        responses:
          '200':
            description: Success
          '400':
            description: Invalid Entry input
  </code></pre>
  
  <p>the Entry definition being:</p>
  
  <pre><code>definitions:
    Entry:
      type: object
      properties:
        firstName:
          type: string
        lastName:
          type: string
        phoneNumber:
          type: string
  </code></pre>
  
  <p>The POST request body look as follow:</p>
  
  <pre><code>{
    "firstName": "John",
    "lastName": "Doe",
    "phoneNumber": "0123456789"
  }
  </code></pre>
  
  <p>This request is handled by a verticle in the following section:</p>
  
  <pre class="lang-java prettyprint-override"><code>vertx.eventBus().&lt;JsonObject&gt; consumer(ADDENTRYBYID_SERVICE_ID)
  .handler(message -&gt; {
    try {
      Phonebook.Entry newEntry = Json.mapper.readValue(message.body().getJsonObject("newEntry").encode(), Phonebook.Entry.class);
  } 
  ..
  }
  </code></pre>
  
  <p>This call fails with the following exception:</p>
  
  <pre class="lang-java prettyprint-override"><code>java.lang.String cannot be cast to io.vertx.core.json.JsonObject
  </code></pre>
  
  <p>when the cast <code>(JsonObject)val</code> is made. </p>
  
  <p>Indeed, val is a <code>String</code> (with value equals to the JSON request body above) , and it can't obviously be casted to a JsonObject. </p>
  
  <p>Rewriting the handler as follow:</p>
  
  <pre class="lang-java prettyprint-override"><code>vertx.eventBus().&lt;String&gt; consumer(ADDENTRYBYID_SERVICE_ID)
  .handler(message -&gt; {
    try {
      Phonebook.Entry newEntry = Json.mapper.readValue(message.body(), Phonebook.Entry.class);
  } 
  ..
  }
  </code></pre>
  
  <p>would also lead to a class cast exception:</p>
  
  <pre class="lang-java prettyprint-override"><code>java.lang.ClassCastException: io.vertx.core.json.JsonObject cannot be cast to java.lang.String
  </code></pre>
  
  <p>These exceptions are thrown by the code below:</p>
  
  <pre class="lang-java prettyprint-override"><code>  public JsonObject getJsonObject(String key) {
      Objects.requireNonNull(key);
      Object val = map.get(key);
      if (val instanceof Map) {
        val = new JsonObject((Map)val);
      }
      return (JsonObject)val;
    }
  </code></pre>
  
  <p>Strangely, <code>message.body()</code>IS a <code>JsonObject</code> instance.</p>
  
  <p><strong><em>Note</em></strong>: <code>JsonObject</code> is a vertx implementation (<code>io.vertx.core.json.JsonObject</code>)</p>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>Why val is a <code>String</code> here ?</li>
  <li>How can I fix this problem ?</li>
  </ol>