Skip to content

Commit

Permalink
feat: exclude userId from serialized nested objects
Browse files Browse the repository at this point in the history
Adding MdxNested annotation that can be added to nested MDX classes
to change behavior. Excluding the UserID in MdxBase in this
iteration. We should consider adding this behavior to serialization.
  • Loading branch information
mattnichols committed Jun 3, 2024
1 parent 3812ad4 commit 256dd8b
Show file tree
Hide file tree
Showing 26 changed files with 104 additions and 9 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
}

version "11.1.0" // x-release-please-version

def platformProject = "platform"
def publishedProjects = [
platformProject,
Expand Down
29 changes: 21 additions & 8 deletions mdx-models/src/main/java/com/mx/path/model/mdx/model/MdxBase.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.mx.path.model.mdx.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Getter;

import com.mx.path.core.common.lang.Strings;
import com.mx.path.core.common.model.ModelBase;
import com.mx.path.core.common.model.Warning;
import com.mx.path.core.context.Session;

public abstract class MdxBase<T> extends ModelBase<T> {
private static final Map<Class<?>, Boolean> INCLUDE_USER_ID_MAP = new HashMap<>();

@Getter
private String userId;

private List<Warning> warnings;
Expand All @@ -19,14 +26,6 @@ public MdxBase() {
}
}

public final String getUserId() {
return this.userId;
}

public final void setUserId(String newUserId) {
this.userId = newUserId;
}

public final List<Warning> getWarnings() {
return this.warnings;
}
Expand All @@ -37,4 +36,18 @@ public final void appendWarning(Warning warning) {
}
this.warnings.add(warning);
}

public final void setUserId(String userId) {
// Don't set the user ID if the model is annotated with MdxNested. Cache result to avoid reflection overhead.
if (!INCLUDE_USER_ID_MAP.containsKey(this.getClass())) {
MdxNested annotation = this.getClass().getAnnotation(MdxNested.class);
INCLUDE_USER_ID_MAP.put(this.getClass(), annotation == null);
}

if (!INCLUDE_USER_ID_MAP.get(this.getClass())) {
return;
}

this.userId = userId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mx.path.model.mdx.model;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Added to MDX model to indicate that the model is typically nested within another model.
*/
@Documented
@Target({ ElementType.TYPE })
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MdxNested {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import lombok.Data;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
@Data
public class HtmlPage extends MdxBase<HtmlPage> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.authorization;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public class JavaScript extends MdxBase<JavaScript> {

private NameValuePair[] arguments;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.authorization;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public class NameValuePair extends MdxBase<NameValuePair> {
private String name;
private String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.google.gson.annotations.SerializedName;
import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Action extends MdxBase<Action> {
private String id;
private String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import com.google.gson.annotations.SerializedName;
import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Button extends MdxBase<Button> {
private String type;
private List<Action> actions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Camera extends MdxBase<Camera> {

private String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.google.gson.annotations.SerializedName;
import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class DeepLinkData extends MdxBase<DeepLinkData> {

@SerializedName("deep_link")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.google.gson.annotations.SerializedName;
import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Format extends MdxBase<Format> {
private String font;
private String color;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;
import com.mx.path.model.mdx.model.authorization.NameValuePair;

@MdxNested
public final class HtmlData extends MdxBase<HtmlData> {

private NameValuePair[] cookies;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Image extends MdxBase<Image> {
private String url;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class InfoData extends MdxBase<InfoData> {
private Format format;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import lombok.EqualsAndHashCode;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
@Data
@EqualsAndHashCode(callSuper = true)
public final class JsonData extends MdxBase<JsonData> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Option extends MdxBase<Option> {
private String id;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import com.google.gson.annotations.SerializedName;
import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Question extends MdxBase<Question> {
private String id;
private String prompt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class ShowChallengeModal extends MdxBase<ShowChallengeModal> {
private Challenge challenge;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class ShowStatus extends MdxBase<ShowStatus> {
private String message;
private String type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.challenges;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Text extends MdxBase<Text> {
private String characterType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.List;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;
import com.mx.path.model.mdx.model.authorization.Authorization;

@MdxNested
public final class MfaChallenge extends MdxBase<MfaChallenge> {

private String answer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mx.path.model.mdx.model.id;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public class MfaChallengeOption extends MdxBase<MfaChallengeOption> {
private String id;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import java.util.List;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public class MfaChallengeQuestion extends MdxBase<MfaChallengeQuestion> {

private String answer;
private String id;
private List<MfaChallengeOption> options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.time.LocalDate;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public final class Location extends MdxBase<Location> {

private String address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mx.path.model.mdx.model

import com.mx.path.core.common.model.Warning
import com.mx.path.model.mdx.model.account.Account
import com.mx.testing.MdxBaseNested

import spock.lang.Specification

Expand Down Expand Up @@ -29,4 +30,17 @@ class MdxBaseTest extends Specification {
then:
warnings.size() == 2
}

def "setUserId"() {
given:
def nestedSubject = new MdxBaseNested()

when:
subject.setUserId("123")
nestedSubject.setUserId("123")

then:
subject.getUserId() == "123"
nestedSubject.getUserId() == null
}
}
8 changes: 8 additions & 0 deletions mdx-models/src/test/java/com/mx/testing/MdxBaseNested.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mx.testing;

import com.mx.path.model.mdx.model.MdxBase;
import com.mx.path.model.mdx.model.MdxNested;

@MdxNested
public class MdxBaseNested extends MdxBase<MdxBaseNested> {
}

0 comments on commit 256dd8b

Please sign in to comment.