amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

[Android] Schema field names must be in camel case, or the generated code fails to build.

Open EdwardFinegan opened this issue 6 years ago • 2 comments

If strict camel case is not follow the generated models will fail to compile do to the capitalization issues. For example if “employeePID” is used as a field name in a model, the generated code will use “employeePID” and “employeePid” interchangeably. This results in a failed build with the error:

error: Builder is not abstract and does not override abstract method employeePID(String) in BuildStep public static class Builder implements BuildStep {

This behavior is also inconsistent with the iOS implementation. The model generation for iOS will handle these names without issue.

Android versions:

implementation 'com.amplifyframework:core:1.3.2'
implementation 'com.amplifyframework:aws-api:1.3.2'

iOS versions:

  - Amplify (1.1.2):
    - Amplify/Default (= 1.1.2)
  - Amplify/Default (1.1.2)
  - Amplify/Tools (1.1.2)
  - AmplifyPlugins/AWSAPIPlugin (1.1.2):
    - AppSyncRealTimeClient (~> 1.4.0)
    - AWSAuthCore (~> 2.15.0)
    - AWSCore (~> 2.15.0)
    - AWSPluginsCore (= 1.1.2)
    - ReachabilitySwift (~> 5.0.0)
  - AmplifyPlugins/AWSDataStorePlugin (1.1.2):
    - AWSAuthCore (~> 2.15.0)
    - AWSCore (~> 2.15.0)
    - AWSPluginsCore (= 1.1.2)
    - SQLite.swift (~> 0.12.0)
  - AppSyncRealTimeClient (1.4.1):
    - Starscream (~> 3.1.0)
  - AWSAuthCore (2.15.3):
    - AWSCore (= 2.15.3)
  - AWSCognitoIdentityProvider (2.15.3):
    - AWSCognitoIdentityProviderASF (= 1.0.2)
    - AWSCore (= 2.15.3)
  - AWSCognitoIdentityProviderASF (1.0.2)
  - AWSCore (2.15.3)
  - AWSMobileClient (2.15.3):
    - AWSAuthCore (= 2.15.3)
    - AWSCognitoIdentityProvider (= 2.15.3)
  - AWSPluginsCore (1.1.2):
    - Amplify (= 1.1.2)
    - AWSCore (~> 2.15.0)
    - AWSMobileClient (~> 2.15.0)
  - CocoaAsyncSocket (7.6.4)
  - ReachabilitySwift (5.0.0)
  - SQLite.swift (0.12.2):
    - SQLite.swift/standard (= 0.12.2)
  - SQLite.swift/standard (0.12.2)
  - Starscream (3.1.1)

Example Model:

type Todo @model
{
   id: ID!
   employeePID: String
}

Generated Model Code:

package com.amplifyframework.datastore.generated.model;

import java.util.List;
import java.util.UUID;
import java.util.Objects;

import androidx.core.util.ObjectsCompat;

import com.amplifyframework.core.model.Model;
import com.amplifyframework.core.model.annotations.Index;
import com.amplifyframework.core.model.annotations.ModelConfig;
import com.amplifyframework.core.model.annotations.ModelField;
import com.amplifyframework.core.model.query.predicate.QueryField;

import static com.amplifyframework.core.model.query.predicate.QueryField.field;

/** This is an auto generated class representing the Todo type in your schema. */
@SuppressWarnings("all")
@ModelConfig(pluralName = "Todos")
public final class Todo implements Model {
  public static final QueryField ID = field("id");
  public static final QueryField EMPLOYEE_PID = field("employeePID");
  private final @ModelField(targetType="ID", isRequired = true) String id;
  private final @ModelField(targetType="String") String employeePID;
  public String getId() {
      return id;
  }
  
  public String getEmployeePid() {
      return employeePID;
  }
  
  private Todo(String id, String employeePID) {
    this.id = id;
    this.employeePID = employeePID;
  }
  
  @Override
   public boolean equals(Object obj) {
      if (this == obj) {
        return true;
      } else if(obj == null || getClass() != obj.getClass()) {
        return false;
      } else {
      Todo todo = (Todo) obj;
      return ObjectsCompat.equals(getId(), todo.getId()) &&
              ObjectsCompat.equals(getEmployeePid(), todo.getEmployeePid());
      }
  }
  
  @Override
   public int hashCode() {
    return new StringBuilder()
      .append(getId())
      .append(getEmployeePid())
      .toString()
      .hashCode();
  }
  
  @Override
   public String toString() {
    return new StringBuilder()
      .append("Todo {")
      .append("id=" + String.valueOf(getId()) + ", ")
      .append("employeePID=" + String.valueOf(getEmployeePid()))
      .append("}")
      .toString();
  }
  
  public static BuildStep builder() {
      return new Builder();
  }
  
  /** 
   * WARNING: This method should not be used to build an instance of this object for a CREATE mutation.
   * This is a convenience method to return an instance of the object with only its ID populated
   * to be used in the context of a parameter in a delete mutation or referencing a foreign key
   * in a relationship.
   * @param id the id of the existing item this instance will represent
   * @return an instance of this model with only ID populated
   * @throws IllegalArgumentException Checks that ID is in the proper format
   */
  public static Todo justId(String id) {
    try {
      UUID.fromString(id); // Check that ID is in the UUID format - if not an exception is thrown
    } catch (Exception exception) {
      throw new IllegalArgumentException(
              "Model IDs must be unique in the format of UUID. This method is for creating instances " +
              "of an existing object with only its ID field for sending as a mutation parameter. When " +
              "creating a new object, use the standard builder method and leave the ID field blank."
      );
    }
    return new Todo(
      id,
      null
    );
  }
  
  public CopyOfBuilder copyOfBuilder() {
    return new CopyOfBuilder(id,
      employeePID);
  }
  public interface BuildStep {
    Todo build();
    BuildStep id(String id) throws IllegalArgumentException;
    BuildStep employeePID(String employeePID);
  }
  

  public static class Builder implements BuildStep {
    private String id;
    private String employeePID;
    @Override
     public Todo build() {
        String id = this.id != null ? this.id : UUID.randomUUID().toString();
        
        return new Todo(
          id,
          employeePID);
    }
    
    @Override
     public BuildStep employeePid(String employeePid) {
        this.employeePID = employeePid;
        return this;
    }
    
    /** 
     * WARNING: Do not set ID when creating a new object. Leave this blank and one will be auto generated for you.
     * This should only be set when referring to an already existing object.
     * @param id id
     * @return Current Builder instance, for fluent method chaining
     * @throws IllegalArgumentException Checks that ID is in the proper format
     */
    public BuildStep id(String id) throws IllegalArgumentException {
        this.id = id;
        
        try {
            UUID.fromString(id); // Check that ID is in the UUID format - if not an exception is thrown
        } catch (Exception exception) {
          throw new IllegalArgumentException("Model IDs must be unique in the format of UUID.",
                    exception);
        }
        
        return this;
    }
  }
  

  public final class CopyOfBuilder extends Builder {
    private CopyOfBuilder(String id, String employeePid) {
      super.id(id);
      super.employeePid(employeePid);
    }
    
    @Override
     public CopyOfBuilder employeePid(String employeePid) {
      return (CopyOfBuilder) super.employeePid(employeePid);
    }
  }
  
}

EdwardFinegan avatar Sep 21 '20 14:09 EdwardFinegan

Similar to https://github.com/aws-amplify/amplify-cli/issues/5046.

richardmcclellan avatar Sep 29 '20 16:09 richardmcclellan

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 25 '21 18:05 github-actions[bot]