Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
managed-swagger = "2.2.0"

kotlintest-runner-junit5 = "3.4.2"
kotlin = "1.6.21"
kotlin = "1.7.0"

[libraries]
managed-swagger-annotations = { module = "io.swagger.core.v3:swagger-annotations", version.ref = "managed-swagger" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ protected <T> T treeToValue(JsonNode jn, Class<T> clazz) throws JsonProcessingEx
T value = jsonMapper.treeToValue(jn, clazz);
if (value != null) {
resolveExtensions(jn).ifPresent(extensions -> BeanMap.of(value).put("extensions", extensions));
// fix for default value
if (jn.has("defaultValue")) {
BeanMap.of(value).put("default", jsonMapper.treeToValue(jn.get("defaultValue"), Map.class));
}
}
return value;
}
Expand Down Expand Up @@ -948,6 +952,9 @@ private void addProperty(Schema parentSchema, String name, Schema propertySchema
private String resolvePropertyName(Element element, Element classElement, Schema propertySchema) {
String name = Optional.ofNullable(propertySchema.getName()).orElse(element.getName());

if (element.hasAnnotation(io.swagger.v3.oas.annotations.media.Schema.class)) {
return element.stringValue(io.swagger.v3.oas.annotations.media.Schema.class, "name").orElse(name);
}
if (element.hasAnnotation(JsonProperty.class)) {
return element.stringValue(JsonProperty.class, "value").orElse(name);
}
Expand Down Expand Up @@ -991,6 +998,96 @@ protected Schema bindSchemaForElement(VisitorContext context, Element element, C
if (schemaDescription.isPresent()) {
schemaToBind.setDescription(schemaDescription.get());
}
Optional<String> schemaFormat = schemaAnn.get("format", String.class);
if (schemaFormat.isPresent()) {
schemaToBind.setFormat(schemaFormat.get());
}
Optional<String> schemaTitle = schemaAnn.get("title", String.class);
if (schemaTitle.isPresent()) {
schemaToBind.setTitle(schemaTitle.get());
}
Optional<BigDecimal> schemaMinimum = schemaAnn.get("minimum", BigDecimal.class);
if (schemaMinimum.isPresent()) {
schemaToBind.setMinimum(schemaMinimum.get());
}
Optional<BigDecimal> schemaMaximum = schemaAnn.get("maximum", BigDecimal.class);
if (schemaMaximum.isPresent()) {
schemaToBind.setMaximum(schemaMaximum.get());
}
Optional<Boolean> schemaExclusiveMinimum = schemaAnn.get("exclusiveMinimum", Boolean.class);
if (schemaExclusiveMinimum.isPresent()) {
schemaToBind.setExclusiveMinimum(schemaExclusiveMinimum.get());
}
Optional<Boolean> schemaExclusiveMaximum = schemaAnn.get("exclusiveMaximum", Boolean.class);
if (schemaExclusiveMaximum.isPresent()) {
schemaToBind.setExclusiveMaximum(schemaExclusiveMaximum.get());
}
Optional<Integer> schemaMinLength = schemaAnn.get("minLength", Integer.class);
if (schemaMinLength.isPresent()) {
schemaToBind.setMinLength(schemaMinLength.get());
}
Optional<Integer> schemaMaxLength = schemaAnn.get("maxLength", Integer.class);
if (schemaMaxLength.isPresent()) {
schemaToBind.setMaxLength(schemaMaxLength.get());
}
Optional<Integer> schemaMinProperties = schemaAnn.get("minProperties", Integer.class);
if (schemaMinProperties.isPresent()) {
schemaToBind.setMinProperties(schemaMinProperties.get());
}
Optional<Integer> schemaMaxProperties = schemaAnn.get("maxProperties", Integer.class);
if (schemaMaxProperties.isPresent()) {
schemaToBind.setMaxProperties(schemaMaxProperties.get());
}
Optional<BigDecimal> schemaMultipleOf = schemaAnn.get("multipleOf", BigDecimal.class);
if (schemaMultipleOf.isPresent()) {
schemaToBind.setMultipleOf(schemaMultipleOf.get());
}
Optional<String> schemaPattern = schemaAnn.get("pattern", String.class);
if (schemaPattern.isPresent()) {
schemaToBind.setPattern(schemaPattern.get());
}

Optional<AnnotationValue<io.swagger.v3.oas.annotations.ExternalDocumentation>> schemaExtDocs = schemaAnn.getAnnotation("externalDocs", io.swagger.v3.oas.annotations.ExternalDocumentation.class);
ExternalDocumentation externalDocs = null;
if (schemaExtDocs.isPresent()) {
externalDocs = toValue(schemaExtDocs.get().getValues(), context, ExternalDocumentation.class).orElse(null);
}
if (externalDocs != null) {
schemaToBind.setExternalDocs(externalDocs);
}
Optional<String> schemaDefaultValue = schemaAnn.get("defaultValue", String.class);
if (schemaDefaultValue.isPresent()) {
try {
schemaToBind.setDefault(jsonMapper.readValue(schemaDefaultValue.get(), Map.class));
} catch (JsonProcessingException e) {
schemaToBind.setDefault(schemaDefaultValue.get());
}
}
Optional<String> schemaExample = schemaAnn.get("example", String.class);
if (schemaExample.isPresent()) {
try {
schemaToBind.setExample(jsonMapper.readValue(schemaExample.get(), Map.class));
} catch (JsonProcessingException e) {
schemaToBind.setExample(schemaExample.get());
}
}
Optional<Boolean> schemaDeprecated = schemaAnn.get("deprecated", Boolean.class);
if (schemaDeprecated.isPresent()) {
schemaToBind.setDeprecated(schemaDeprecated.get());
}
Optional<io.swagger.v3.oas.annotations.media.Schema.AccessMode> schemaAccessMode = schemaAnn.get("accessMode", io.swagger.v3.oas.annotations.media.Schema.AccessMode.class);
if (schemaAccessMode.isPresent()) {
if (schemaAccessMode.get() == io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY) {
schemaToBind.setReadOnly(true);
schemaToBind.setWriteOnly(null);
} else if (schemaAccessMode.get() == io.swagger.v3.oas.annotations.media.Schema.AccessMode.WRITE_ONLY) {
schemaToBind.setReadOnly(false);
schemaToBind.setWriteOnly(null);
} else if (schemaAccessMode.get() == io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE) {
schemaToBind.setReadOnly(null);
schemaToBind.setWriteOnly(null);
}
}
Comment on lines +1078 to +1090
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not critical but this code can be written more cleanly with something like:

schemaAnn.enumValue("accessMode", io.swagger.v3.oas.annotations.media.Schema.AccessMode.class)
   .ifPresent(accessMode -> {
          switch(accessMode) {
               case READ_ONLY:
                        schemaToBind.setReadOnly(true);
                         schemaToBind.setWriteOnly(null);
                break;
               case WRITE_ONLY:
                        schemaToBind.setReadOnly(false);
                         schemaToBind.setWriteOnly(null);
                break;
          }
   });

Apart from that is setting write only to null correct?

Copy link
Collaborator Author

@altro3 altro3 Jun 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graemerocher I don't now, sorry. This block copypasted from swagger lib

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graemerocher Your proposal is not so simple, because would require making the schemaToBind variable finalized. In this case, I think that this block is better left as is.

изображение

}
}
if (originalSchema.get$ref() == null && schemaAnn != null) {
Expand Down
Loading