Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Created Enum types have private field "name" but no accessor #286

Closed
leflamm opened this issue Jul 13, 2017 · 5 comments
Closed

Created Enum types have private field "name" but no accessor #286

leflamm opened this issue Jul 13, 2017 · 5 comments

Comments

@leflamm
Copy link

leflamm commented Jul 13, 2017

Hi, we're using

<groupId>org.raml</groupId>
<artifactId>raml-to-jaxrs-maven-plugin</artifactId>
<version>2.0.0</version>

to create resources from our raml definition. Problem is: all created Enum types have a private field name containing the actual enum value from the raml file, but there's no accessor for it.

public enum BucketType {
  RANGE("range"),

  STRING("string");

  private String name;

  BucketType(String name) {
    this.name = name;
  }
}
@jpbelang
Copy link
Contributor

I'm curious as to why you need an accessor for the name, as it's equal to the enum value ? it's mostly equal to enum.name().toLowerCase().

(You may quite rightly tell me it's non of my business, but I'm curious).

This said, tonight I'll see if I can show you how to make a plugin to generate the accessor.

@leflamm
Copy link
Author

leflamm commented Jul 13, 2017

(You may quite rightly tell me it's non of my business, but I'm curious).

:-)

  1. A general remark: why put extra information (field name) into an Enum element if nobody can access it? Even my IDE (Eclipse in stock error/warning configuration) warns: "The value of the field PriceType.name is not used".

  2. A specific example: Consider this generated Enum type.

public enum PriceType {
  NETLIST("net_list");

// ...
  
  private String name;

  PriceType(String name) {
    this.name = name;
  }
}

If I want to serialize an element of this type with Jackson or something similar into JSON it will serialize NETLIST into "NETLIST". If I need serialization into the exact value that is specified in the RAML file I could register a type-specific serializer that would access and use the name field which contains the desired value "net_list". Unfortunately I cannot because this field is not accessible.

So at the moment, as a work-around, I'm using reflection to force-access the information from that private field, but, to me, that seems unnecessarily complicated.

I see these options:

  • ditch the inaccessible field, forget conventions and name the actual Enum elements exactly like they are named in the RAML file (even if they aren't all-caps, contain underscores, ...)
  • provide an accessor for the actual Enum element name

Cheers, Christian

@jpbelang
Copy link
Contributor

I'd like to ditch the incacessible field, honestly. it was put there for reasons I don't remember now. The actual raml name doesn't look like it's necessary.

(sorry for the delay, am currently working on builder/emitter for raml).

@giacgbj
Copy link

giacgbj commented Oct 26, 2017

In my opinion, the names should be the same as specified in the RAML, without removing the underscores.

@uweschaefer
Copy link

you really need to be able to access exactly the string taken from RAML, which in version 2.1.0 is the name-field. If it is the SomeEnum.name(), fine but anything else make it unusable for us.

@jstoiko jstoiko closed this as completed Dec 7, 2017
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

5 participants