Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add camelcase option to schema type generation #215

Closed
wants to merge 1 commit into from

Conversation

MbBrainz
Copy link

@MbBrainz MbBrainz commented Jan 5, 2024

This pr is regarding the implementation of the feature request in this issue: #213

This pr adds the following features:

  • optional config parameter useCamelCasedProps
  • camelcasing of schema type parameters.

The new config param useCamelCasedProps has the following effect on the schema:

    const schema: SchemaObject = {
      type: "object",
      properties: {
        ["foo_bar"]: {
          type: "string",
        },
      },
    };

// resulting type for `useCamelCasedProps == false` (as usual)
export type Test = {
          foo_bar?: string;
}

// resulting type for `useCamelCasedProps == true` with the new addition
export type Test = {
          fooBar?: string;
}

This pr is still in DRAFT because:

With the current state of this pr the fetchers will fail to get parse the received json correctly, as the received data will be in snake_case and the types to cast to would be in camelCase. A solution to make this work, would be a configurable adjustment to the fetcher template (./plugins/typescript/src/core/templates/fetcher.ts) that applies camelCase to incoming request data and that applies snake_case to outgoing data. (exact implementation is still to be figured out)

@fabien0102 Please let me know whether you would be interested in having this feature added, and if so i can try to spend the additional time needed to make this work. Any tips/comments/improvements are very welcome!

@MbBrainz
Copy link
Author

@fb55 @fabien0102 @micha-f What do you think about this feature

@fabien0102
Copy link
Owner

Sorry for the late answer, I don’t think this feature is wanted here.

The "best" way to handle this, is to mutate the openapi schema before generation (you have some example in plugins/typescript/src/utils

The idea will be something like this:

// openapi-codegen.config.ts
import {
  generateSchemaTypes,
  generateReactQueryComponents,
} from "@openapi-codegen/typescript";
import { defineConfig } from "@openapi-codegen/cli";

export default defineConfig({
  github: {
    from: {
      source: "url",
      url: "https://api.apis.guru/v2/specs/github.com/1.1.4/openapi.yaml",
    },
    outputDir: "src/github",
    to: async (context) => {
      // HERE!
     camelizedAllOptions(context.openAPIDocument); // This function mutate the schema directly
      const filenamePrefix = "github";
      const { schemasFiles } = await generateSchemaTypes(context, {
        filenamePrefix,
      });
      await generateReactQueryComponents(context, {
        filenamePrefix,
        schemasFiles,
      });
    },
  },
});

And after, you need to deal with this in the generated ...Fetcher.ts to ensure everything is handled correctly.

I used this kind of setup in the past, it works well.

@fabien0102 fabien0102 closed this Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants