Skip to content

Commit

Permalink
refs-#4703/#4702-Fix applyBeanValidatorAnnotations method to support …
Browse files Browse the repository at this point in the history
…both OAS 3.1/3.0
  • Loading branch information
micryc committed Sep 19, 2024
1 parent b1a38d6 commit 7601ca6
Show file tree
Hide file tree
Showing 18 changed files with 856 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public ModelConverters(boolean openapi31) {
}
}

public ModelConverters(boolean openapi31, Schema.SchemaResolution schemaResolution) {
converters = new CopyOnWriteArrayList<>();
if (openapi31) {
converters.add(new ModelResolver(Json31.mapper()).openapi31(true));
} else {
converters.add(new ModelResolver(Json.mapper()).schemaResolution(schemaResolution));
}
}

public Set<String> getSkippedPackages() {
return skippedPackages;
}
Expand All @@ -61,6 +70,30 @@ public static ModelConverters getInstance(boolean openapi31) {
return SINGLETON;
}

public static void reset() {
synchronized (ModelConverters.class) {
SINGLETON = null;
SINGLETON31 = null;
}
}

public static ModelConverters getInstance(boolean openapi31, Schema.SchemaResolution schemaResolution) {
synchronized (ModelConverters.class) {
if (openapi31) {
if (SINGLETON31 == null) {
SINGLETON31 = new ModelConverters(openapi31);
init(SINGLETON31);
}
return SINGLETON31;
}
if (SINGLETON == null) {
SINGLETON = new ModelConverters(openapi31, schemaResolution);
init(SINGLETON);
}
return SINGLETON;
}
}

private static void init(ModelConverters converter) {
converter.addPackageToSkip("java.lang");
converter.addPackageToSkip("groovy.lang");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public class ModelResolver extends AbstractModelConverter implements ModelConver

private boolean openapi31;

private Schema.SchemaResolution schemaResolution = Schema.SchemaResolution.DEFAULT;

public ModelResolver(ObjectMapper mapper) {
super(mapper);
}
Expand Down Expand Up @@ -725,7 +727,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
property = context.resolve(aType);
property = clone(property);
if (openapi31) {
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, context);
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, schemaResolution, context);
if (reResolvedProperty.isPresent()) {
property = reResolvedProperty.get();
}
Expand Down Expand Up @@ -773,10 +775,14 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
}

if (context.getDefinedModels().containsKey(pName)) {
property = new Schema().$ref(constructRef(pName));
if (Schema.SchemaResolution.INLINE.equals(this.schemaResolution)) {
property = context.getDefinedModels().get(pName);
} else {
property = new Schema().$ref(constructRef(pName));
}
property = clone(property);
if (openapi31) {
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, context);
if (openapi31 || Schema.SchemaResolution.INLINE.equals(this.schemaResolution)) {
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, this.schemaResolution, context);
if (reResolvedProperty.isPresent()) {
property = reResolvedProperty.get();
}
Expand Down Expand Up @@ -1000,7 +1006,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
StringUtils.isNotBlank(model.getName()))
{
if (context.getDefinedModels().containsKey(model.getName())) {
model = new Schema().$ref(constructRef(model.getName()));
if (!Schema.SchemaResolution.INLINE.equals(this.schemaResolution)) {
model = new Schema().$ref(constructRef(model.getName()));
}
}
} else if (model != null && model.get$ref() != null) {
model = new Schema().$ref(StringUtils.isNotEmpty(model.get$ref()) ? model.get$ref() : model.getName());
Expand Down Expand Up @@ -1255,6 +1263,18 @@ private void handleUnwrapped(List<Schema> props, Schema innerModel, String prefi
}
}

public Schema.SchemaResolution getSchemaResolution() {
return schemaResolution;
}

public void setSchemaResolution(Schema.SchemaResolution schemaResolution) {
this.schemaResolution = schemaResolution;
}

public ModelResolver schemaResolution(Schema.SchemaResolution schemaResolution) {
setSchemaResolution(schemaResolution);
return this;
}

private class GeneratorWrapper {

Expand Down Expand Up @@ -1491,43 +1511,38 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
if ("integer".equals(property.getType()) || "number".equals(property.getType())) {
property.setMinimum(new BigDecimal(size.min()));
property.setMaximum(new BigDecimal(size.max()));
} else if (property instanceof StringSchema) {
StringSchema sp = (StringSchema) property;
sp.minLength(Integer.valueOf(size.min()));
sp.maxLength(Integer.valueOf(size.max()));
} else if (property instanceof ArraySchema) {
ArraySchema sp = (ArraySchema) property;
sp.setMinItems(size.min());
sp.setMaxItems(size.max());
}
if (isStringSchema(property)) {
property.setMinLength(Integer.valueOf(size.min()));
property.setMaxLength(Integer.valueOf(size.max()));
}
if (isArraySchema(property)) {
property.setMinItems(size.min());
property.setMaxItems(size.max());
}
}
if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
if (property instanceof NumberSchema) {
NumberSchema ap = (NumberSchema) property;
ap.setMinimum(new BigDecimal(min.value()));
ap.setExclusiveMinimum(!min.inclusive());
if (isNumberSchema(property)) {
property.setMinimum(new BigDecimal(min.value()));
property.setExclusiveMinimum(!min.inclusive());
}
}
if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
if (property instanceof NumberSchema) {
NumberSchema ap = (NumberSchema) property;
ap.setMaximum(new BigDecimal(max.value()));
ap.setExclusiveMaximum(!max.inclusive());
if (isNumberSchema(property)) {
property.setMaximum(new BigDecimal(max.value()));
property.setExclusiveMaximum(!max.inclusive());
}
}
if (annos.containsKey("javax.validation.constraints.Pattern")) {
Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");

if (property instanceof StringSchema) {
if (isStringSchema(property)) {
property.setPattern(pattern.regexp());
}

if(property.getItems() != null && property.getItems() instanceof StringSchema) {
if(property.getItems() != null && isStringSchema(property.getItems())) {
property.getItems().setPattern(pattern.regexp());
}

}
}

Expand Down Expand Up @@ -1870,7 +1885,7 @@ protected Map<String, Schema> resolveDependentSchemas(JavaType a, Annotation[] a
}
Annotation[] propAnnotations = new Annotation[]{dependentSchemaAnnotation.schema(), dependentSchemaAnnotation.array()};
Schema existingSchema = null;
Optional<Schema> resolvedPropSchema = AnnotationsUtils.getSchemaFromAnnotation(dependentSchemaAnnotation.schema(), components, jsonViewAnnotation, openapi31, null, context);
Optional<Schema> resolvedPropSchema = AnnotationsUtils.getSchemaFromAnnotation(dependentSchemaAnnotation.schema(), components, jsonViewAnnotation, openapi31, null, Schema.SchemaResolution.DEFAULT, context);
if (resolvedPropSchema.isPresent()) {
existingSchema = resolvedPropSchema.get();
dependentSchemas.put(name, existingSchema);
Expand Down Expand Up @@ -2989,6 +3004,18 @@ protected boolean isObjectSchema(Schema schema) {
return (schema.getTypes() != null && schema.getTypes().contains("object")) || "object".equals(schema.getType()) || (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
}

protected boolean isArraySchema(Schema schema){
return "array".equals(schema.getType()) || (schema.getTypes() != null && schema.getTypes().contains("array"));
}

protected boolean isStringSchema(Schema schema){
return "string".equals(schema.getType()) || (schema.getTypes() != null && schema.getTypes().contains("string"));
}

protected boolean isNumberSchema(Schema schema){
return "number".equals(schema.getType()) || (schema.getTypes() != null && schema.getTypes().contains("number"));
}

protected Schema buildRefSchemaIfObject(Schema schema, ModelConverterContext context) {
if (schema == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,24 +587,40 @@ public static Optional<Schema> getSchemaFromAnnotation(
boolean openapi31,
Schema existingSchema,
ModelConverterContext context) {
return getSchemaFromAnnotation(schema, components, jsonViewAnnotation, openapi31, existingSchema, Schema.SchemaResolution.DEFAULT, null);
}
public static Optional<Schema> getSchemaFromAnnotation(
io.swagger.v3.oas.annotations.media.Schema schema,
Components components,
JsonView jsonViewAnnotation,
boolean openapi31,
Schema existingSchema,
Schema.SchemaResolution schemaResolution,
ModelConverterContext context) {
if (schema == null || !hasSchemaAnnotation(schema)) {
if (existingSchema == null || !openapi31) {
if (existingSchema == null || (!openapi31 && Schema.SchemaResolution.DEFAULT.equals(schemaResolution))) {
return Optional.empty();
} else if (existingSchema != null && openapi31) {
} else if (existingSchema != null && (openapi31 || Schema.SchemaResolution.INLINE.equals(schemaResolution))) {
return Optional.of(existingSchema);
}
}
Schema schemaObject = null;
if (!openapi31) {
if (existingSchema != null) {
return Optional.of(existingSchema);
if (!Schema.SchemaResolution.DEFAULT.equals(schemaResolution)) {
schemaObject = existingSchema;
} else {
return Optional.of(existingSchema);
}
}
if (schema.oneOf().length > 0 ||
schema.allOf().length > 0 ||
schema.anyOf().length > 0) {
schemaObject = new ComposedSchema();
} else {
schemaObject = new Schema();
if (Schema.SchemaResolution.DEFAULT.equals(schemaResolution)) {
if (schema != null && (schema.oneOf().length > 0 ||
schema.allOf().length > 0 ||
schema.anyOf().length > 0)) {
schemaObject = new ComposedSchema();
} else {
schemaObject = new Schema();
}
}
} else {
if (existingSchema == null) {
Expand All @@ -613,6 +629,9 @@ public static Optional<Schema> getSchemaFromAnnotation(
schemaObject = existingSchema;
}
}
if (schema == null) {
return Optional.of(schemaObject);
}
if (StringUtils.isNotBlank(schema.description())) {
schemaObject.setDescription(schema.description());
}
Expand Down Expand Up @@ -1956,11 +1975,15 @@ public static void updateAnnotation(Class<?> clazz, io.swagger.v3.oas.annotation

}

public static Annotation mergeSchemaAnnotations(
Annotation[] ctxAnnotations, JavaType type) {
return mergeSchemaAnnotations(ctxAnnotations, type, false);
}
/*
* returns null if no annotations, otherwise either ArraySchema or Schema
*/
public static Annotation mergeSchemaAnnotations(
Annotation[] ctxAnnotations, JavaType type) {
Annotation[] ctxAnnotations, JavaType type, boolean contextWins) {
// get type array and schema
io.swagger.v3.oas.annotations.media.Schema tS = type.getRawClass().getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
if (!hasSchemaAnnotation(tS)) {
Expand Down Expand Up @@ -2020,6 +2043,9 @@ else if (tS != null && tA == null && cS == null && cA != null) {
}

else if (tA != null && cA != null) {
if (contextWins) {
return mergeArraySchemaAnnotations(tA, cA);
}
return mergeArraySchemaAnnotations(cA, tA);
}

Expand All @@ -2028,6 +2054,9 @@ else if (tS != null && cS == null && cA == null) {
}

else if (tS != null && cS != null) {
if (contextWins) {
return mergeSchemaAnnotations(cS, tS);
}
return mergeSchemaAnnotations(tS, cS);
}

Expand Down
Loading

0 comments on commit 7601ca6

Please sign in to comment.