Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for hidden field in JsonCreator #3698

Merged
merged 4 commits into from Nov 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,9 @@
package io.swagger.v3.core.resolving;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
Expand All @@ -10,6 +13,7 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

public class HiddenFieldTest {
Expand All @@ -29,9 +33,12 @@ public void testHiddenField() {

final Schema nameValue = (Schema) model.getProperties().get("name");
assertTrue(nameValue instanceof StringSchema);

final Schema passwordValue = (Schema) model.getProperties().get("password");
assertNull(passwordValue);
}

class ModelWithHiddenFields {
static class ModelWithHiddenFields {
@io.swagger.v3.oas.annotations.media.Schema(required = true)
public Long id = null;

Expand All @@ -41,4 +48,44 @@ class ModelWithHiddenFields {
@io.swagger.v3.oas.annotations.media.Schema(hidden = true)
public String password = null;
}

@Test(description = "it should ignore a hidden field in @JsonCreator")
public void testHiddenFieldInJsonCreator() {
final Map<String, Schema> models = ModelConverters.getInstance().read(ModelWithHiddenFieldsInJsonCreator.class);

final Schema model = models.get("ModelWithHiddenFieldsInJsonCreator");
assertNotNull(model);
assertEquals(model.getProperties().size(), 1);

final Schema idValue = (Schema) model.getProperties().get("id");
assertTrue(idValue instanceof IntegerSchema);

final Schema hiddenValue = (Schema) model.getProperties().get("hidden");
assertNull(hiddenValue);
}

static class ModelWithHiddenFieldsInJsonCreator {
@JsonProperty("id")
private final Long id;

@io.swagger.v3.oas.annotations.media.Schema(hidden = true)
@JsonProperty("hidden")
private final String hidden;

@JsonCreator
public ModelWithHiddenFieldsInJsonCreator(@JsonProperty("id") Long id,
@JsonProperty("hidden") @io.swagger.v3.oas.annotations.media.Schema(hidden = true) String hidden) {
this.id = id;
this.hidden = hidden;
}

public Long getId() {
return id;
}

@io.swagger.v3.oas.annotations.media.Schema(hidden = true)
public String getHidden() {
return hidden;
}
}
}