spring-auto-restdocs icon indicating copy to clipboard operation
spring-auto-restdocs copied to clipboard

Enum constraints and request parameters that are collections

Open cmercer opened this issue 6 years ago • 7 comments

On version 1.0.14 because we have not upgraded to spring 5 yet.

This looks like a combination of 181and 194 issues.

I have the case where I have an EnumSet as a parameter but I have tried this with List, Set and Arrays and they all come out the same.

public enum Color {
  BLACK,
  WHITE
}

Using the Color enum with an EnumSet as a parameter.

 @RequestMapping(method = RequestMethod.POST)
  public Set<Color> selectColors(
      @CurrentSpringUser SpringUser springUser,
      @RequestParam(name = "colors") EnumSet<Color> colors
  {
      return colors;
  }

What it comes out as is

Parameter Type Optional Description
expand Object true

I would expect it to come out as

Parameter Type Optional Description
expand Array[String] true Must be one or more of [BLACK,WHITE]

Also need to deal with the case of the Enum having a different "name" than the Enum

public enum Colors {
  BLACK("Black"),
  WHITE("White")
}

becomes

Parameter Type Optional Description
expand Array[String] true Must be one or more of [Black,White]

Additionally this should work with List, Set and Array not just EnumSet.

I checked 2.X but I didn't see anything that looks like it would have fixed this issue.

cmercer avatar Jan 03 '19 04:01 cmercer

Hi, looks like we need to support EnumSet collections specifically, should not be a problem. Regarding different "name", how exactly should it behave? You added arbitrary String field to that enum, how do we know that it's the json representation of the enum?

jmisur avatar Jan 07 '19 09:01 jmisur

There are a few situations I think have to be dealt with relating to having a different "name"

  1. Just an Enum
public enum Color {
  BLACK,
  WHITE
}
  1. Enum with a name, this one is a little bit tricky too because the name can have spaces in it.
public enum Crayon {
  BLACK("Black Crayon"),
  WHITE("White Crayon")
}
  1. Annotated for Jackson with @JsonProperty
public enum Crayon {
  @JsonProperty("Black Crayon")
  BLACK,
  @JsonProperty("White Crayon")
  WHITE
}

More complicated things I don't have quick examples for :) 4. @JsonValue and @JsonCreator 5. @JsonSerialize and @JsonDeserialize 6. Different configuration settings ie WRITE_ENUMS_USING_TO_STRING, READ_ENUMS_USING_TO_STRING

Most of these should just work since it comes from Jackson in spring-auto-restdocs, but dealing with different versions and future proofing might want to have test cases for these.

I will hopefully have some time to write some examples next week.

cmercer avatar Jan 10 '19 04:01 cmercer

Hi, any examples which we could use to include in our test suite and be able to fix the underlying problem? You can also submit a PR.

jmisur avatar Mar 17 '19 12:03 jmisur

I have been dealing with other things and this has been a low priority, I hoping to have it moved up in priority next week so I can write some test cases and maybe actually contribute support.

cmercer avatar Mar 19 '19 18:03 cmercer

Sorry this has taken so long, actually working on another issue.

cmercer avatar Apr 04 '19 21:04 cmercer

Hi all,

Is there any way to disable Auto enum description in Restdocs?

ankit45621 avatar Apr 12 '19 07:04 ankit45621

@ankit45621 Currently no.

jmisur avatar May 06 '19 08:05 jmisur