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

Types generated by platformatic client of a Record<string, array> gives an generic object as result. "additionalProperties" are omited of OpenAPI specs #2353

Open
EnriqueJMP-holcim opened this issue Apr 4, 2024 · 0 comments
Assignees

Comments

@EnriqueJMP-holcim
Copy link

Hi. Im facing some problems trying to generate the api-types.d.ts of a Record<string, array> response. But the client always generate an empty "object" as result. Im using "platformatic": "^1.32.0",

 platformatic client http://localhost:3000/documentation/json --frontend --language ts

Given this pseudo-schema:

{
"recipesGroupedByPhases": {
  "description": "Recipes grouped by phases",
  "type": "object",
  "additionalProperties": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "phase": {
          ...
        }
        "header": {
          ...
        }
        "billOfMaterials": {
          ...
        }
      },
      "required": [
        "phase",
        "header",
        "billOfMaterials"
      ]
    }
  }
}

//Example value:
"recipesGroupedByPhases": {
  "someKey": [
    {
      "phase": {},
      "header": {},
      "billOfMaterials": []
    }
  ]
}

I was trying to debug and found where was the problem. I tried some workaround and it generates the types as I expected but maybe the code that I implemented needs some tweaks:

// Object without properties

// File: @platformatic>client-cli>lib>get-type.mjs>getType
// Line of code: 74
  if (typeDef.type === 'object') {
    if (!typeDef.properties || Object.keys(typeDef.properties).length === 0) {
      //===>START of the workaround
      if (typeDef.additionalProperties) {
        return `{ [key: string]: ${getType(typeDef.additionalProperties, methodType, spec)} }`
      }
      //<===END of the workaround
      // Object without properties
      return 'object'
    }
    let output = '{ '
    // TODO: add a test for objects without properties
    /* c8 ignore next 1 */
    const props = Object.keys(typeDef.properties || {}).map((prop) => {
      let required = false
      if (typeDef.required) {
        required = !!typeDef.required.includes(prop)
      }
      return `'${prop}'${required ? '' : '?'}: ${getType(typeDef.properties[prop], methodType, spec)}`
    })

Generated types without applying the workaround:

recipesGroupedByPhases: object

After the workaround:

recipesGroupedByPhases: {
        [key: string]: {
            phase: string;
            header: {
                'id': string;
                'taskListGroup': string;
                'groupCounter': string;
                'productVersion': string;
                'plant': string;
                'material': string;
            };
            billOfMaterials: Array<{
                'operationNumber': string;
                'material': string;
                'materialDetails'?: {
                    'materialID': string;
                    'descriptions': Array<{
                        'languageKey': string;
                        'language': string;
                        'text': string;
                    }>;
                };
            }>;
        }[];
    };
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

No branches or pull requests

2 participants