openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Cannot generate Typescript fetch index.ts supporting files independently

Open alwaysbemark opened this issue 4 years ago • 7 comments

Bug Report Checklist

  • [ ] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [ ] Have you searched for related issues/PRs?
  • [ ] What's the actual output vs expected output?
  • [x] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

For the Typescript Fetch generator, I need to be able to generate index.ts files inside the models directory without having to generate apis at all. Currently this is not possible because of what seems to be an oversight or a bug.

openapi-generator version

5.2.1

OpenAPI declaration file content or url

Irrelevant.

Generation Details
openApiGenerate {
    generatorName = "typescript-fetch"
    inputSpec = "foo/bar.yml"
    outputDir = "$projectDir/src"
    configOptions = [
            supportsES6: "true",
            typescriptThreePlus: "true"
    ]
    globalProperties = [
            apis: "",
            models: "",
            supportingFiles: "index.ts"
    ]
}
Steps to reproduce

Use the above configuration in Gradle to generate Typescript fetch code. This works beautifully but generates both the apis and models directory. I don't want the former, so I delete apis: "" from the configuration above and the nested index.ts file inside models fails to generate properly.

Related issues/PRs

Haven't seen any.

Suggest a fix

Pretty sure I see what the problem is:

The index.ts file I'm after is being generated by method TypeScriptFetchClientCodegen::postProcessOperationsWithModels. This method is only invoked by the parent class DefaultGenerator here. Unfortunately this method is skipped if we indicate that we don't want to generate apis here. This seems to create this coupling between the generation of the apis and models index.ts supporting file.

Sponsorship

I'd be happy to sponsor this fix for $100 @wing328

alwaysbemark avatar Sep 01 '21 08:09 alwaysbemark

Added sponsorship section cc @wing328 . Thanks so much for OpenAPI codegen!

alwaysbemark avatar Sep 01 '21 08:09 alwaysbemark

What about the other way around by using .openapi-generator-ignore to skip files you don't want to generate?

Ref: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#ignore-file-format

wing328 avatar Sep 01 '21 08:09 wing328

Yup that'll work although it provides a less elegant solution for us in some ways. I'm leaving this open in case this is needed by anyone since it looks like that dependency should be broken. Thanks a lot!

alwaysbemark avatar Sep 02 '21 22:09 alwaysbemark

openapi-geneator-ignore was implemented by @jimschubert . You can support his work directly via https://www.patreon.com/jimschubert

wing328 avatar Sep 04 '21 14:09 wing328

Thank you! Will look into sponsoring even if this doesn't get resolved. Happy to close this if you think that it isn't worth it!

alwaysbemark avatar Sep 05 '21 22:09 alwaysbemark

Hi everyone, this issue seems to be still present. I tried to suppress generation via .openapi-generator.ignore, but the API files are still generated.

I'm using the maven plugin (version 6.0.1), placed a .openapi-generator-ignore in my output folder and set <ignoreFileOverride /> in my pom.xml.

The .openapi-generator-ignore contains

/**/apis # `./apis` is in the root folder of `output`.

In my pom.xml:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.0.1</version>
    <executions>
        <execution>
            <id>generate-openapi-client</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.build.directory}/openapi.json</inputSpec>
                <output>../../frontend/src/client</output>
                <generatorName>typescript-fetch</generatorName>
                <ignoreFileOverride>.openapi-generator-ignore</ignoreFileOverride>
            </configuration>
        </execution>
    </executions>
</plugin>

Did I do something wrong? I tried to circumvent it by simply removing the folder on prepare-package, but the index.ts needs to be updated as ./apis is removed. On Windows, it's a bit tedious due to access locks on index.ts when working with an IDE.

kereis avatar Aug 11 '22 09:08 kereis

Hi, I am wondering if this is gonna be addressed outside of the .openapi-generator-ignore file as the workaround has a price. Namely, by adding some lines to the ignore file like below:

**/apis/*Api.ts
**/apis/index.ts
**/apis/

The result is that the files are not generated (great!) but the folder is still there. Worse, the src/index.ts file contains the following import

export * from './apis';

Leading to tsc complaining.

src/index.ts:2:15 - error TS2307: Cannot find module './apis'.

2 export * from './apis';
                ~~~~~~~~

Of course we can add more workaround. tsc || true or remove that line in post processing. But this is a lot of work for a workaround. What do you think about that?

rafspiny avatar Feb 09 '24 15:02 rafspiny