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

Make ObjectMapper used for finding properties configurable #4776

Open
T3rm1 opened this issue Nov 8, 2024 · 0 comments
Open

Make ObjectMapper used for finding properties configurable #4776

T3rm1 opened this issue Nov 8, 2024 · 0 comments

Comments

@T3rm1
Copy link

T3rm1 commented Nov 8, 2024

The problem
Swagger creates its own ObjectMapper instance for serializing schemas. In the process of resolving a model, this object mapper is used to find the properties of the target class.

List<BeanPropertyDefinition> properties = beanDesc.findProperties();

Applications always create their own ObjectMapper to serialize their API response objects. This object mapper might have different settings than the one from Swagger. An example is different visibility settings for a PropertyAccessor.

Consider the following example:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
System.out.println(objectMapper.writeValueAsString(response));
public class Response {
    private String value;
    // getter and setter for value

    public String getSomething() {
        return "There should be no property 'something' in the generated schema";
    }
}

Restricting serialzation of classes to their fields and ignoring getters is a common thing. When the application serializes the object, only value will appear in its result.

This is different to the generated schema from Swagger, which will also include a property something. This is due to Swagger using its own ObjectMapper and calling findProperties (see above).

"Response": {
  "properties": {
    "value": {
      "type": "string"
    },
    "something": {
      "type": "string"
    }
  }
}

Expectation
Swagger should allow to specify an ObjectMapper that is used for finding properties of a bean class. Calling findProperties with this ObjectMapper returns only visible properties.

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