swagger-api / swagger-api/swagger-core

Make `ObjectMapper` used for finding properties configurable

Open
#4,776 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backlog
Dominant language
Java
Stars
7.5k
Forks
2.3k
Avg merge
18h 1m
Merged PRs (30d)
10

Description

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.

https://github.com/swagger-api/swagger-core/blob/dc8785efc71c243eebf1ad57dd612cc75112d351/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java#L584

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java at the referenced findProperties call. Trace how the resolver's ObjectMapper is created and identify the existing configuration or extension path. Done means callers can provide an ObjectMapper whose visibility settings control the generated schema, including the field-only example described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.