redskap / swagger-brake

Swagger contract checker for breaking API changes
Apache License 2.0
58 stars 16 forks source link

Can you provide a simple entrypoint to compare 2 API instances #16

Closed dalewking closed 5 years ago

dalewking commented 5 years ago

Since you are splitting out the CLI from the core library, was hoping you could provide a simple entry point that takes in 2 OpenAPI instances and returns the list of breaking changes.

Here is the code I am currently doing on my side, but you could do it much easier in the library:

private static Collection<BreakingChange> invokeSwaggerBrake(@NotNull OpenAPI oldApi, @NotNull OpenAPI newApi) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
            RunnerConfiguration.class, ReporterConfiguration.class,
            MavenConfiguration.class, CoreConfiguration.class);

    final String beanName = context.getBeanNamesForType(
            ResolvableType.forClassWithGenerics(Transformer.class, OpenAPI.class, Specification.class))[0];

    @SuppressWarnings("unchecked")
    final Transformer<OpenAPI, Specification> transformer
            = (Transformer<OpenAPI, Specification>)context.getBean(beanName);

    return context.getBean(BreakChecker.class).check(transformer.transform(oldApi), transformer.transform(newApi));
}

Should be easy if you extract this expression on Runner to a new method called check:

breakChecker.check(transformer.transform(oldApi), transformer.transform(newApi));

Extract everything before .run(options) in Starter.start to a getRunner method. Then add this method to Starter:

public static Collection<BreakingChange> check(@NotNull OpenAPI oldApi, @NotNull OpenAPI newApi) {
    return getRunner().check(oldApi, newApi);
}
galovics commented 5 years ago

Done. Please check if it suits your use-case. :-)

dalewking commented 5 years ago

Was looking for a static method to call that hid all the dependency injection from me the way Stater.start does. What you have here requires me to still instantiate the Context to get the Checker.

galovics commented 5 years ago

I mean if I provide a static method which does all the configuration in advance, it's undermining the modularity of swagger-brake. Now you want to use only a a single component of Swagger Brake so you have to do the configuration for sure.

Is it really a big issue that you have to do the configuration?

dalewking commented 5 years ago

I don't see how it is undermining modularity. You provide that with Starter.start. All I am asking for is Starter to look like this:

public class Starter {
    private static BeanFactory getBeanFactory() {
        return new AnnotationConfigApplicationContext(RunnerConfiguration.class, ReporterConfiguration.class,
                MavenConfiguration.class, CoreConfiguration.class);
    }

    public static Collection<BreakingChange> start(Options options) {
        return getBeanFactory().getBean(Runner.class).run(options);
    }

    public static Collection<BreakingChange> check(OpenAPI oldApi, OpenAPI newApi) {
        return getBeanFactory().getBean(Checker.class).check(oldApi, newApi);
    }
}
galovics commented 5 years ago

You got it @dalewking. :-)