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

Propagate JsonView annotation when resolving schema properties #4719

Open
gabrielhof opened this issue Aug 20, 2024 · 0 comments
Open

Propagate JsonView annotation when resolving schema properties #4719

gabrielhof opened this issue Aug 20, 2024 · 0 comments

Comments

@gabrielhof
Copy link

gabrielhof commented Aug 20, 2024

I'm currently using Jackson's @JsonView annotation to include/exclude class properties based on the API operation that is being performed (e.g., create, update, read, etc.).

I have a specific need where I must annotate a class with @SchemaProperties, @SchemaProperty, and @Schema. However, the ModelResolver (Jackson-specific implementation of ModelConverter) ends up not propagating the @JsonView annotation when resolving the classes of the properties defined with @SchemaProperties. This causes the resulting schema to include properties that should be excluded for certain operations.

Map<String, Schema> schemaProperties = resolveSchemaProperties(type, annotatedType.getCtxAnnotations(), context);

protected Map<String, Schema> resolveSchemaProperties(JavaType a, Annotation[] annotations, ModelConverterContext context) {

Example

public class CommonViews {

    public static class Update { }

}

@PatchMapping("/update")
public void update(@JsonView(CommonViews.Update.class) AnnotatedWithSchemaProperties payload) {
    // logic
}

@SchemaProperties({
    @SchemaProperty(
        name = "my_custom_property",
        schema = @Schema(implementation = PropertyAnnotatedWithJsonView.class)
   )
})
public class AnnotatedWithSchemaProperties {
}

public class PropertyAnnotatedWithJsonView {

  private String alwaysIncludedProp;

  @JsonView(CommonViews.Update.class)
  private String includedOnlyWhenUpdating;

}

Alternative Solution

I was able to overcome the problem for now by defining a "dummy" setter method, causing the schema to be generated the way I'd expected:

@JsonProperty("my_custom_property")
public void setMyCustomProperty(PropertyAnnotatedWithJsonView myCustomProperty) {
    // logic
}

Additional Context

  • I'm using Swagger Core via Springdoc (which can be assumed based on the example above)
  • In case it is helpful, I'm using @SchemaProperties/@SchemaProperty/@Schema in a class that allows dynamic properties to be provided by consumers via the @JsonAnySetter/@JsonAnyGetter annotations. @SchemaProperties/... allow me to provide some examples in the generated schema to our consumers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant