swagger-api / swagger-api/swagger-core

ModelResolver.resolveExample() returns JsonNode but DateTimeSchema.cast() expects a String

Open
#4,363 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

I have this property in my POJO:

    @Schema(description = "Start of the shift as timestamp using ISO-8601 representation.",
        example = "\"2023-02-17T08:30:00Z\"")
    private Instant start;

When io.swagger.v3.core.jackson.ModelResolver.resolveExample() parses this, it returns a JsonNode (actually, TextNode):

                    ObjectMapper mapper = ObjectMapperFactory.buildStrictGenericObjectMapper();
                    return mapper.readTree(schema.example());
```

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

This is then passed to `DateTimeSchema.cast()` which expects a String or Date or OffsetDateTime
https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/DateTimeSchema.java#L37

Suggestions for a fix:

- Change the code to try specific Date types. If that doesn't work, use value.toString() and try to parse that
- Add JsonNode to the type switch.

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 with ModelResolver.resolveExample() in modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java and DateTimeSchema.cast() in modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/DateTimeSchema.java. Reproduce the reported Instant example and trace the JsonNode/TextNode value between them. Done means the example is handled consistently for the reported date-time case without breaking existing accepted value types.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.