View Javadoc

1   package com.atlassian.rest.annotation;
2   
3   import com.google.common.collect.ImmutableSet;
4   
5   import java.lang.annotation.ElementType;
6   import java.lang.annotation.Retention;
7   import java.lang.annotation.RetentionPolicy;
8   import java.lang.annotation.Target;
9   import java.util.Set;
10  
11  /**
12   * This annotation can give hints to the JSON schema generator about a bean property.
13   */
14  @Retention (RetentionPolicy.RUNTIME)
15  @Target ({ ElementType.FIELD, ElementType.METHOD })
16  public @interface RestProperty
17  {
18      public static enum Scope
19      {
20          /**
21           * for fields available only in requests
22           */
23          REQUEST,
24          /**
25           * for fields available only in responses
26           */
27          RESPONSE,
28          /**
29           * for fields available in requests and in responses
30           */
31          BOTH,
32          /**
33           * if the field name is {@code self} or {@code expand} then RESPONSE, otherwise BOTH
34           */
35          AUTO;
36  
37          private final static Set<String> responseScopeByDefault = ImmutableSet.of("self", "expand");
38  
39          public boolean contains(Scope scope)
40          {
41              return this == BOTH || this == AUTO || this == scope;
42          }
43  
44          public boolean includes(Scope fieldScope, String fieldName)
45          {
46              fieldScope = resolveAuto(fieldScope, fieldName);
47              return fieldScope == BOTH || fieldScope == this || this == BOTH || this == AUTO;
48          }
49  
50          private static Scope resolveAuto(Scope scope, String fieldName)
51          {
52              if (scope == AUTO)
53              {
54                  return responseScopeByDefault.contains(fieldName) ? RESPONSE : BOTH;
55              }
56              else
57              {
58                  return scope;
59              }
60          }
61      }
62  
63      /**
64       * Sets the scope for the field.
65       *
66       * <p>
67       *  Each schema is generated in one of two scopes: REQUEST and RESPONSE.
68       *  Use this annotation to control whether the field should appear in a scope.
69       * </p>
70       *
71       */
72      Scope scope() default Scope.AUTO;
73  
74      /**
75       * Property pattern for {@code Map} fields.
76       */
77      String pattern() default ".+";
78  }