UpCloudLtd / upcloud-java-api

Java client for UpCloud's API
MIT License
4 stars 2 forks source link

Server State - Null Ptr or "UNKNOWN_ATTRIBUTE" #6

Open michaelfclarke opened 6 years ago

michaelfclarke commented 6 years ago

I am trying to use the API to create a server, by cloning a template. I believe I am doing the "right" thing, based on this documentation: https://www.upcloud.com/api/1.2.6/8-servers/#create-server

Essentially this is the code I have:

        ServerApi serverAPI = new ServerApi(client);

        Server server = new Server();
        server.setZone("uk-lon1");
        server.setTitle(fullServerName);
        server.setHostname(fullServerName);
        server.setCoreNumber(new BigDecimal(2));
        server.setMemoryAmount(new BigDecimal(2048));

        ServerStorageDevices devices = new ServerStorageDevices();
        StorageDevice sd = new StorageDevice();
        sd.setAction("clone");
        sd.setStorage("014a041c-7eb3-48fb-9c7c-e09e70c8274f");
        sd.setTitle(fullServerName);
        sd.setSize(new BigDecimal(50));
        sd.setTier("maxiops");
        sd.setPartOfPlan(PartOfPlanEnum.YES);
        devices.addStorageDeviceItem(sd);
        server.setStorageDevices(devices);
        server.setBootOrder(BootOrderEnum.DISK);
        server.passwordDelivery(PasswordDeliveryEnum.NONE);

        CreateServerRequest createServerRequest = new CreateServerRequest();
        createServerRequest.setServer(server);

        return serverAPI.createServer(createServerRequest);

However, there is an issue when actually sending the request to the RESTful API. If I do not include a ServerState value on the Server object, I get a Null Pointer Exception:

2018-03-07 15:35:33,292 ERROR [http-nio-8086-exec-6] () Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at com.upcloud.client.models.ServerState$Adapter.write(ServerState.java:63) at com.upcloud.client.models.ServerState$Adapter.write(ServerState.java:60) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99) ...

If I include a ServerState on the Server object, I get an "UNKNOWN_ATTRIBUTE" response from the server:

2018-03-07 15:06:12,453 INFO [http-nio-8086-exec-5] () Request Content-type: application/json Bad Request { "error" : { "error_code" : "UNKNOWN_ATTRIBUTE", "error_message" : "The request body contains an unknown attribute state." } }

michaelfclarke commented 6 years ago

As a side note, I have fixed the issue locally with the following pattern:

Replacing this code where you have Enums:

public void write(final JsonWriter jsonWriter, final ServerState enumeration) throws IOException {
        jsonWriter.value(enumeration.getValue());
}

With the following:

public void write(final JsonWriter jsonWriter, final ServerState enumeration) throws IOException {
  if (enumeration != null) {
        jsonWriter.value(enumeration.getValue());
  } else {
        jsonWriter.nullValue();
  }
}

This change worked for me, allowing me to optionally provide Server State (and some other Enums that were affected). So, I can submit a pull request with these changes - if you would like?

iler commented 6 years ago

@michaelfclarke thanks for reporting this in! And please do submit a PR if you have fixed this issue :)

michaelfclarke commented 5 years ago

Finally got around to sending in a pull request - only a year later!