sudorandom / protoc-gen-connect-openapi

Plugin for generating OpenAPIv3 from protobufs matching the Connect RPC interface
MIT License
102 stars 7 forks source link

Remove unused schemas #31

Closed sudorandom closed 1 month ago

sudorandom commented 2 months ago

Right now it's possible for schemas to be unused but still exist in the openapi spec. I think we can do some tree-shaking to ensure that unreferenced schemas get removed before we render it to the user. We can use the index/rolodex feature of libopenapi to help with this:

    {
        b, err := spec.Render()
        if err != nil {
            return nil, err
        }
        doc, err := libopenapi.NewDocument(b)
        if err != nil {
            return nil, err
        }
        model, errs := doc.BuildV3Model()
        if errs != nil {
            return nil, errors.Join(errs...)
        }
        model.Index.BuildIndex()
        slog.Info("rolo", "errs", model.Model.Rolodex.GetCaughtErrors())
        for name, ref := range model.Model.Rolodex.GetRootIndex().GetAllCombinedReferences() {
            slog.Info(name, "ref", ref)
        }
    }
sudorandom commented 2 months ago

Another strategy is to only add schemas when they are referenced (and don't already exist). That would give us this same effect, and may be easier to support other features later on

sudorandom commented 1 month ago

Should this be an option? It may be useful to see all of the types even if they aren't directly used.

sudorandom commented 1 month ago

This option is added with v0.12.0 as trim-unused-types