swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Allow programmatic model name customization

Open T3rm1 opened this issue 11 months ago • 0 comments

Introduce a method that can be overriden, or another mechanism, to allow for model name customization. An example would be to have a suffix "Enum" for all enum models resolved as ref.

Doing this with a custom model resolver doesn't work.

public class EnumModelConverter implements ModelConverter {

	@Override
	public Schema<?> resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
		if (chain.hasNext()) {
			if (type.getType() instanceof JavaType javaType) {
				if (javaType.isEnumType()) {
					if (!javaType.getRawClass().getSimpleName().endsWith("Enum")) {
						type.setName(javaType.getRawClass().getSimpleName() + "Enum");
					}
				}
			} else if (type.getType() instanceof Class<?> clazz) {
				if (clazz.isEnum()) {
					if (!clazz.getSimpleName().endsWith("Enum")) {
						type.setName(clazz.getSimpleName() + "Enum");
					}
				}
			}
			return chain.next().resolve(type, context, chain);
		}
		return null;
	}
}

While this seems to work, it fails as soon as you have an enum with a custom name in its Schema annotation.

@Schema(name = "SomeClassStatus")
public enum Status {
    ONE, TWO
}

Proposal: Add a protected method after https://github.com/swagger-api/swagger-core/blob/3c7b61f7d51bb001ec3bc944229cbc19307d17f5/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java#L213 that allows to customize the name.

T3rm1 avatar Nov 19 '24 10:11 T3rm1