-
Notifications
You must be signed in to change notification settings - Fork 59
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
Support the "format" string tag without the other OpenAPI format-values #396
Comments
Hi @magicDGS, The
With
I understand you'd like to see the Long story short: one must know all possible data types for which a particular format is desired. Therefore, my preference so far is to recommend this being done through configuration. Map<Class<?>, String> typesWithFormat = Map.of(
UUID.class, "uuid", URI.class, "uri", LocalDateTime.class, "date-time"
);
configBuilder.forTypesInGeneral()
.withStringFormatResolver(scope -> typesWithFormat.get(scope.getType().getErasedType())); However, this is only meaningful for types resolved as configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES, Option.ADDITIONAL_FIXED_TYPES);
// augment standard options, which are always applied last
configBuiilder.with(new SimpleTypeModule()
// override default definition, that includes undesired "format" values
.withIntegerType(Integer.class).withIntegerType(int.class)
.withIntegerType(Long.class).withIntegerType(long.class)
// add missing "format" values
.withStringType(java.time.Duration.class, "duration")
.withStringType(java.time.Period.class, "duration")
); That being said, I'm open to discussing which types should get which "format" value by default and adding a new |
Hello @CarstenWickner - thanks for the timely answer. When I was using the library on a playground to test it for my project, I realized that there is no way to include those "format" properties without having a custom module/format-resolver as you said (I have already used the So to be clear, what I expect is the following (adding the @JsonPropertyOrder({
"aInt", "aLong", "aFloat", "aDouble",
"aLocalDate",
"aLocalDateTime", "aZonedDateTime", "aOffsetDateTime", "aInstant", "aDate", "aCalendar",
"aLocalTime", "aOffsetTime",
"aDuration", "aPeriod",
"aUuid", "aUri"
})
private static class ExpectedBuiltInFormats {
///////////////////
// No built-in on the jsonschema specs
// (but OpenAPI defined)
//////////////////
public Integer aInt; // expected: none (OpenAPI: int32)
public Long aLong; // expected: none (OpenAPI: int64)
public Float aFloat; // expected: none (OpenAPI: float)
public Double aDouble; // expected: none (OpenAPI: double)
///////////////////
// Dates and times
///////////////////
public java.time.LocalDate aLocalDate; // expected: date
public java.time.LocalDateTime aLocalDateTime; // expected: date-time
public java.time.ZonedDateTime aZonedDateTime; // expected: date-time
public java.time.OffsetDateTime aOffsetDateTime; // expected: date-time
public java.time.Instant aInstant; // expected: date-time
public java.util.Date aDate; // expected: date-time
public java.util.Calendar aCalendar; // expected: date-time
public java.time.LocalTime aLocalTime; // expected: time
public java.time.OffsetTime aOffsetTime; // expected: time
public Duration aDuration; // expected: duration
public Period aPeriod; // expected: duration
///////////////////
// Resource identifiers
///////////////////
public java.util.UUID aUuid; // expected: uuid
public java.net.URI aUri; // expected: uri
} What it was misleading and might be a good addition to the library is to handle the @Test
public void testWithAdditionalFixedTypes() {
final SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON)
.with(new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_ORDER))
.with(Option.ADDITIONAL_FIXED_TYPES) // might be also posible to use a different option for this use-case
.build();
final ObjectNode node = new SchemaGenerator(config)
.generateSchema(ExpectedBuiltInFormats.class);
org.assertj.core.api.Assertions.assertThat(node.toPrettyString())
.isEqualToIgnoringNewLines(EXPECTED_SCHEMA);
} Where the {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"aInt": {
"type": "integer"
},
"aLong": {
"type": "integer"
},
"aFloat": {
"type": "number"
},
"aDouble": {
"type": "number"
},
"aLocalDate": {
"type": "string",
"format": "date"
},
"aLocalDateTime": {
"type": "string",
"format": "date-time"
},
"aZonedDateTime": {
"type": "string",
"format": "date-time"
},
"aOffsetDateTime": {
"type": "string",
"format": "date-time"
},
"aInstant": {
"type": "string",
"format": "date-time"
},
"aDate": {
"type": "string",
"format": "date-time"
},
"aCalendar": {
"type": "string",
"format": "date-time"
},
"aLocalTime": {
"type": "string",
"format": "time"
},
"aOffsetTime": {
"type": "string",
"format": "time"
},
"aDuration": {
"type": "string",
"format": "duration"
},
"aPeriod": {
"type": "string",
"format": "duration"
},
"aUuid": {
"type": "string",
"format": "uuid"
},
"aUri": {
"type": "string",
"format": "uri"
}
}
} I think this won't be too difficult as they are already handled on the OpenAPI integration. If you accept contributions, I am also available to refactor to re-use the current definitions, add the |
HI @magicDGS, Your proposals sound good:
I'd be happy to review and merge a PR for these changes (or whatever part you have time for). |
Hello @CarstenWickner - the PR is ready: #399 - I also updated the documentation and added some tests. The only think that I don't fully understand is what you mean with the |
As per your separate question: this has been released as part of v4.33.0 now. |
When looking into how to get the
"format"
tag properly formatted following the specs https://json-schema.org/understanding-json-schema/reference/string#format, I found that the only supported option for that is theOption.EXTRA_OPEN_API_FORMAT_VALUES
. Nevertheless, this introduces"format"
tags also for properties of numeric types that are not defined in the json-schema specs.I know that there is a way to implement this with a customized method, but it would be nice to have support with an
Option
to use the supported built-in formats (https://json-schema.org/understanding-json-schema/reference/string#built-in-formats) from the json-schema specs (without relying on third-party OpenAPI custom formats).Is there a possibility to support that feature out-of-the-box with either an option or directly when a version schema supports that built-in format?
Reproduction: I realized when I tried to use the
2019-09
schema with a model containing anUUID
and anInteger
, expecting to either get a"format": uuid
with some option (i.e.,Option.ADDITIONAL_FIXED_TYPES
) but the only way was providing the OpenAPI format values that are also adding the"format": int32
to thenumber
andinteger
fields.The text was updated successfully, but these errors were encountered: