0

I have a Cloud Endpoints method that begins like this:

@ApiMethod(httpMethod = HttpMethod.POST, path = "kokoers")
public Kokoer createKokoer(Kokoer kokoer, User user) throws UnauthorizedException, BadRequestException {
    logger.info(new ObjectMessage("kokoer: " + kokoer));

I am testing it with curl like this:

curl --request POST --header "content-Typ: application/json" -H "Authorization: Bearer <JWT token>" --data '{"kokoerName":"Danny Dwob", "emailAddress":"me@there"}' http://localhost:8080/_ah/api/kokodokoapi/v1/kokoers/ 

The logged message from my method is:

09:32:08.940 [qtp2009787198-57] INFO  uk.co.scapps.kokodoko.api.KokodokoAPI - kokoer: Kokoer [kokoer_id=null, kokoerName=null, no. of kokos=0, periods=[], emailAddress=null, kokoSharingPolicy=null]

I was expecting that kokoerName and emailAddress would have non-null values.

The path definition in my openapi.json file looks like this:

"paths": {
  "/kokodokoapi/v1/kokoers": {
     "post": {
        "operationId": "KokodokoapiCreateKokoer",
        "parameters": [
           {
              "in": "body",
              "name": "body",
              "required": false,
              "schema": {
                 "$ref": "#/definitions/Kokoer"
              }
           }
        ],
        "responses": {
           "200": {
              "description": "A successful response",
              "schema": {
                 "$ref": "#/definitions/Kokoer"
              }
           }
        },
        "security": [
           {
              "firebase": []
           }
        ],
        "x-security": [
           {
              "firebase": {
                 "audiences": [
                    "<project id>"
                 ]
              }
           }
        ]
     }
  },

and the Kokoer definition in that file looks like this:

"Kokoer": {
         "properties": {
            "emailAddress": {
               "type": "string"
            },
            "kokoSharingPolicy": {
               "$ref": "#/definitions/KokoSharingPolicy_E"
            },
            "kokoerName": {
               "type": "string"
            },
            "kokoer_id": {
               "type": "integer",
               "format": "int64"
            },
            "kokos": {
               "type": "array",
               "items": {
                  "$ref": "#/definitions/Koko"
               }
            },
            "periods": {
               "type": "array",
               "items": {
                  "$ref": "#/definitions/KokoPeriod"
               }
            }
         }
      }

The relevant (as I understand it; happy to be corrected) portions of the Kokoer class definition look like this:

package uk.co.scapps.kokodoko.datamodel;

import java.util.HashSet;
import java.util.Set;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity(name="Kokoer")
@Table(name="kokoer")
public class Kokoer {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(updatable = false, nullable = false)
    private Long kokoer_id;
    private String kokoerName;

    @OneToMany(mappedBy = "kokoer")
    @JsonManagedReference
    private Set<Koko> kokos = new HashSet<Koko>();

. . .

    public String getKokoerName() {
        return this.kokoerName;
    }

    public void setKokoerName(String kokoerName) {
        this.kokoerName = kokoerName;
    }

    public Kokoer() {
        super();
        // TODO Auto-generated constructor stub
    }

}

The method otherwise runs entirely as expected given that the input Kokoer object has all null field values instead of the values I am passing in from curl.

I have one other API method in the project so far for creating objects of a similar type. It is structured in an extremely similar way, and it works as expected including the input values from curl appearing in the input object. I cannot spot a structural difference between the two to explain the different behavior.

Any thoughts?

Thanks.

-Dan

Dan Stone
  • 23
  • 6

2 Answers2

1

No code changes. Working today (after re-starting machine). Stumped.

Update: More to report here. Had a recurrence of the problem where setters aren't being called to initialize entity on post methods.

Wanted to see the content of the request body, but stream can only be read once. So, added this filter:

https://gist.github.com/calo81/2071634

and now the API method works (values are being set).

If I comment out the filter in web.xml, my API method stops working again, and when I re-activate the filter my API method again works as expected. This has happened consistently over several iterations.

Although I have seen the same problem on the cloud app engine (standard v1), so far I have only tried the filter work-around on the local app engine.

Dan Stone
  • 23
  • 6
0

The problem could be the fact you miss-spelled Content-Type as "Content-Typ" above. I have found Post calls will not set variables if the Content-Type is not set to application/json.