OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.01k stars 6.6k forks source link

[REQ] Allow generating only src/main/scala in sttp generator #6685

Open ghostbuster91 opened 4 years ago

ghostbuster91 commented 4 years ago

The idea is very similar to https://github.com/OpenAPITools/openapi-generator/issues/6155. I would like to create a compiler plugin for sbt which generates only scala classes from openapi specification and puts them on the classpath.

As far as I got familiar with openApi generators while working on #6684 it seems to me that it might be quite easy to achieve that by just adding a single if statement into the sttpClientCode generator.

So the change would affect this method where we add templates to process:

    @Override
    public void processOpts() {
        super.processOpts();
        properties.forEach(p -> p.updateAdditionalProperties(additionalProperties));
        invokerPackage = INVOKER_PACKAGE.getValue(additionalProperties);
        apiPackage = API_PACKAGE.getValue(additionalProperties);
        modelPackage = MODEL_PACKAGE.getValue(additionalProperties);

        supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
        supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));
        final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator);
        supportingFiles.add(new SupportingFile("jsonSupport.mustache", invokerFolder, "JsonSupport.scala"));
        supportingFiles.add(new SupportingFile("project/build.properties.mustache", "project", "build.properties"));
        supportingFiles.add(new SupportingFile("dateSerializers.mustache", invokerFolder, "DateSerializers.scala"));
}

First of all I might be wrong and it is not so simple after all. The second question is whether it should be done for the rest of scala generators or even for all generators.

@clasnake (2017/07), @jimschubert (2017/09) heart, @shijinkui (2018/01), @ramzimaalej (2018/03), @chameleon82 (2020/03), @Bouillie (2020/04)

jimschubert commented 4 years ago

@ghostbuster91 we already support this using the ignore file.

From the CLI perspective, you can try this out with:

cat > .bak/ignore-file <<EOF
*
**/*
!**/src/main/scala/**/*
EOF

openapi-generator generate -g scala-sttp \
-i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
-o .bak/scala-sttp-scala-only \
--ignore-file-override "$(pwd)"/.bak/ignore-file

tree .bak/scala-sttp-scala-only
.bak/scala-sttp-scala-only
└── src
    └── main
        └── scala
            └── org
                └── openapitools
                    └── client
                        ├── api
                        │   ├── EnumsSerializers.scala
                        │   ├── PetApi.scala
                        │   ├── StoreApi.scala
                        │   └── UserApi.scala
                        ├── core
                        │   ├── ApiInvoker.scala
                        │   ├── Serializers.scala
                        │   └── requests.scala
                        └── model
                            ├── ApiResponse.scala
                            ├── Category.scala
                            ├── InlineObject.scala
                            ├── InlineObject1.scala
                            ├── Order.scala
                            ├── Pet.scala
                            ├── Tag.scala
                            └── User.scala

9 directories, 15 files

Although, I'm not entirely sure what you intend to do as far as a scala compiler plugin. That seems like overkill and I'd recommend either using our SBT plugin (see https://github.com/OpenAPITools/sbt-openapi-generator/), or just wiring this up directly if you have more complex needs (see my example at https://github.com/jimschubert/sbt-local-generator).

ghostbuster91 commented 4 years ago

Thanks for the answer.

By scala compiler plugin I meant sbt-plugin just like the one you linked (incorrect use of words, sorry).

It took me a while to reply because while it looks like something we were looking for I wanted to check if it fulfills all our requirements.

From my tests it looks like despite explicitly saying that we are only interested in src/main/scala openapi-generator generates also .openapi-generator-ignore and .openapi-generator/VERSION files which aren't shown on your output because they are prefixed with a dot.

This prevents us from plugging openapiGenerator task into sbt sourceGenerators because all generated files have to be compile-able:

    Compile / sourceGenerators += openApiGenerate

Also to use it correctly with sbt we should be able to generate these files into src_managed directory and there is no option to tell the plugin to do that (to change src into src_managed)

For more information refer to: https://www.scala-sbt.org/1.x/docs/Howto-Generating-Files.html