mrdannael / genum-openapi

Generate Typescript enums from the openapi schema
MIT License
3 stars 2 forks source link

Custom replacers not working #8

Closed borzaka closed 2 months ago

borzaka commented 2 months ago

I have this (extracted) schema:

---
openapi: 3.0.1
components:
  schemas:
    GeographicLocation:
      required:
      - '@type'
      type: object
      properties:
        '@type':
          type: string
          description: The name of the GeoJSON structure used in the geometry attribute
          enum:
          - GeoJsonPoint
          - GeoJsonMultiPoint
          - GeoJsonLineString
          - GeoJsonMultiLineString
          - GeoJsonPolygon
        bbox:
          type: array
          description: A bounding box array that contains the geometry. The axes order follows the axes order of the geometry
          items:
            type: number
      description: "A GeographicLocation is a pure-virtual super-class to the GeoJSON-aligned geometries of Point (addresses and locations), MultiPoint, LineString (streets, highways and boundaries), MultiLineString and Polygon (countries, provinces, tracts of land). Use the @type attribute to specify which of these is being specified by the geometry attribute."

When generating, I have this error:

src/lib/api/cart-enums.ts
[error] src/lib/api/cart-enums.ts: SyntaxError: '{' expected. (367:33)
[error]   365 | }
[error]   366 |
[error] > 367 | export enum GeographicLocation__@type {
[error]       |                                 ^
[error]   368 |   GEOJSONPOINT = "GeoJsonPoint",
[error]   369 |   GEOJSONMULTIPOINT = "GeoJsonMultiPoint",
[error]   370 |   GEOJSONLINESTRING = "GeoJsonLineString",

I tried to replace the @ with the custom replacers, but with no luck.

This is my command line:

genum-openapi ./src/openapi/cart.yaml -o src/lib/api/cart-enums.ts --with-parent --normalize-keys --uppercase-keys --custom-replacers '[{"regExp":"[@]","replaceWith":"_"}]'

And also when I tried without --normalize-keys and use just the custom replacers, it didn't replace any. I used the one in the example: '[{"regExp":"[-/]","replaceWith":"_"}]' It didn't replace any enums with - in it.

So my guess is that the --custom-replacers is not working as intended. Or am I missing something?

borzaka commented 2 months ago

Well, I have figured out what was I missing: --custom-replacers only works with --normalize and not with --normalize-keys

So my final working CLI:

genum-openapi ./src/openapi/cart.yaml -o src/lib/api/cart-enums.ts --with-parent --normalize --uppercase-keys --custom-replacers '[{"regExp":"[@]","replaceWith":"_"}]'
mrdannael commented 2 months ago

Yup. If I remember correctly --normalize-names should work too in your case. It should normalize exported enums names. --normalize works on both exported names and enum keys.