Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Implement restrictive deserialization
Browse files Browse the repository at this point in the history
Following the API guidelines, we're implementing a restrictive
deserialization strategy https://opensource.zalando.com/restful-api-guidelines/#109
  • Loading branch information
rcillo committed Jun 28, 2018
1 parent 734da2d commit 66eb25f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/zalando/nakadi/config/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Enum deserialize(final JsonParser jp, final DeserializationContext ctxt)

try {
if (rawClass.equals(Audience.class)) {
return Audience.valueOf(jpValueAsString.toUpperCase().replaceAll("-", "_"));
return Audience.fromString(jpValueAsString);
} else {
return Enum.valueOf(rawClass, jpValueAsString.toUpperCase());
}
Expand Down Expand Up @@ -109,7 +109,7 @@ private static class LowerCaseEnumJsonSerializer extends StdSerializer<Enum> {
public void serialize(final Enum value, final JsonGenerator jgen, final SerializerProvider provider)
throws IOException {
if (value.getClass().equals(Audience.class)) {
jgen.writeString(value.name().toLowerCase().replaceAll("_", "-"));
jgen.writeString(((Audience)value).getText());
} else {
jgen.writeString(value.name().toLowerCase());
}
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/org/zalando/nakadi/domain/Audience.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package org.zalando.nakadi.domain;

public enum Audience {
COMPONENT_INTERNAL, BUSINESS_UNIT_INTERNAL, COMPANY_INTERNAL, EXTERNAL_PARTNER, EXTERNAL_PUBLIC
COMPONENT_INTERNAL("component-internal"),
BUSINESS_UNIT_INTERNAL("business-unit-internal"),
COMPANY_INTERNAL("company-internal"),
EXTERNAL_PARTNER("external-partner"),
EXTERNAL_PUBLIC("external-public");

private String text;

Audience(final String text) {
this.text = text;
}

public String getText() {
return this.text;
}

public static Audience fromString(final String text) {
for (final Audience audience : Audience.values()) {
if (audience.text.equals(text)) {
return audience;
}
}
throw new IllegalArgumentException();
}
}

0 comments on commit 66eb25f

Please sign in to comment.