Skip to content
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

How to override @Deprecated? #747

Closed
wtell400 opened this issue Jun 24, 2020 · 3 comments
Closed

How to override @Deprecated? #747

wtell400 opened this issue Jun 24, 2020 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@wtell400
Copy link

I have a situation in which one of my setters has a @java.lang.Deprecated annotation on an entity. The field itself does not have this annotation.

In swagger, in my schema/model, this fields ends up with the deprecated: true text. I did not expect that, and would like to avoid that.

public class DeprecatedEntity
{
    @Schema(deprecated = false) // Does not work
    private String myNonDeprecatedField;

    public String getMyNonDeprecatedField()
    {
        return myNonDeprecatedField;
    }

    @Schema(deprecated = false) // Does not work
    @Deprecated
    public DeprecatedEntity setMyNonDeprecatedField(String myNonDeprecatedField)
    {
        this.myNonDeprecatedField = myNonDeprecatedField;
        return this;
    }
}

I have tried overriding this with @Schema(deprecated = false) on both the setter and the field, but this does not seem to work. I assume that processing @Deprecated is done by the springdoc library, although I'm not sure what part is springdoc and what part is swagger.

Using springdoc-openapi 1.4.1. See attached project.
springdoc-issue-override-deprecated.zip

@bnasslahsen
Copy link
Collaborator

@wtell400,

First solution, you override SchemaPropertyDeprecatingConverter:

@Component
public class DeprecatedPropertyCustomizer extends SchemaPropertyDeprecatingConverter {

	@Override
	public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
		if (chain.hasNext()) {
			Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
			if (type.isSchemaProperty() && containsDeprecatedAnnotation(type.getCtxAnnotations()) && !"myNonDeprecatedField".equals(type.getPropertyName()))
				resolvedSchema.setDeprecated(true);
			return resolvedSchema;
		}
		return null;
	}

}

Second solution, you use OpenApiCustomiser:

@Bean
public OpenApiCustomiser sortSchemasAlphabetically() {
	return openApi -> {
		if (openApi.getComponents() != null) {
			Schema schema = openApi.getComponents().getSchemas().get("DeprecatedEntity");
			Map<String, Schema> properties = schema.getProperties();
			Schema myNonDeprecatedFieldSchema = properties.get("myNonDeprecatedField");
			myNonDeprecatedFieldSchema.setDeprecated(false);
		}
	};
}

@wtell400
Copy link
Author

wtell400 commented Jun 29, 2020

Thank you for the solutions.

These solutions are based on the class and/or field name. I am trying to avoid duplicating 'magic' names and references to a field in other classes. If I would change the field name, then this functionality would stop working if I forget about it.

The whole idea of using Swagger and @Schema is to keep the documentation close to the class/field. Is there a better way of doing this, for example with @Schema(deprecated = true) on the same class/method/field that has @Deprecated?

@bnasslahsen
Copy link
Collaborator

bnasslahsen commented Jun 29, 2020

The whole idea of springdoc-openapi is to get your documentation the closest to the code, with minimal code changes.
If the code contains @Deprecated, sprindoc-openapi will consider its schema as Deprecated as well.

Here, you want to declare a field on swagger as non deprecated, even with the java code, the field contains @Depreacted!

Additionally to the previous mentioned solutions, you can use the following property that will be available with the next release v1.4.3:

springdoc.model-converters.deprecating-converter.enabled=false

This property, will disable the @Deprecated resolution from springdoc-openapi. So you can use @Schema(deprecated = ...), wherever you want.

@bnasslahsen bnasslahsen added enhancement New feature or request question Further information is requested labels Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants