tomasz-mer
tomasz-mer

Reputation: 3930

Create a schema from Java class based on Smallrye OpenAPI

I have an custom OASFilter implementation and want to add some schema of complex object to created Open API document.

Here is a ComplexType:

public class ComplexType
    {
        private OtherComplexType field;

        public OtherComplexType getField()
        {
            return field;
        }
    
        public void setField( OtherComplexType field )
        {
            this.field = field;
        }
    }

And OASFilter implementation:

@OpenApiFilter( OpenApiFilter.RunStage.BOTH )
public class CustomFilter  implements OASFilter
{
    private final IndexView view;

    public CustomFilter( IndexView view )
    {
        this.view = view;
    }

    @Override
    public void filterOpenAPI( OpenAPI openAPI )
    {
        AnnotationScannerContext context = new AnnotationScannerContext(view, ClassLoaderUtil.getDefaultClassLoader(),
            emptyConfig());
        Type type =
            Type.create( DotName.createSimple( ComplexType.class ), Type.Kind.CLASS );
        Schema result = SchemaFactory.typeToSchema(context, type, null, Collections.emptyList());
        openAPI.getComponents().addSchema( "complexType", result );
    }

    public static OpenApiConfig emptyConfig() {
        return OpenApiConfig.fromConfig(new SmallRyeConfigBuilder()
            .build());
    }
}

As a result I have almost an empty schema - only with a type of Schema, without properties Do I do it correctly? Can it be done differently? What I would like to avoid is writing my own model resolver.

Upvotes: 0

Views: 89

Answers (0)

Related Questions