Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Optional Enum field is not supported #115

Open
akomisarek opened this issue Jun 5, 2018 · 1 comment
Open

Optional Enum field is not supported #115

akomisarek opened this issue Jun 5, 2018 · 1 comment

Comments

@akomisarek
Copy link

akomisarek commented Jun 5, 2018

For following input schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "id": "http://cpp.moj.gov.uk/sjp/test-enum",
  "properties": {
    "enumField": {
      "enum": ["FIRST_OPTION", "SECOND_OPTION"]
    }
  },
  "additionalProperties": false
}

The generator generated following class:

package uk.gov.moj.cpp.sjp;

import java.lang.Override;
import java.lang.String;
import java.util.Optional;

public enum EnumField {
  FIRST_OPTION("FIRST_OPTION"),

  SECOND_OPTION("SECOND_OPTION");

  private final String value;

  EnumField(String value) {
    this.value = value;
  }

  @Override
  public String toString() {
    return value;
  }

  public static Optional<Optional<EnumField>> valueFor(final String value) {
    if(FIRST_OPTION.value.equals(value)) { return Optional.of(FIRST_OPTION); };
    if(SECOND_OPTION.value.equals(value)) { return Optional.of(SECOND_OPTION); };
    return Optional.empty();
  }
}

Which doesn't compile, because of valueFor method. Expected result is code which compiles, possibly returning just Optional as the additional Optional seems unnecessary.

@akomisarek
Copy link
Author

akomisarek commented Jun 14, 2018

I did quick investigation, actually it is caused by the plugin: SupportJavaOptionalsPlugin.

EnumGenerator buildValueForMethod uses createsType with additional Optional which is wrong.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant