typescript-rest-swagger icon indicating copy to clipboard operation
typescript-rest-swagger copied to clipboard

Trouble generating additionalProperties

Open tnrich opened this issue 5 years ago • 2 comments

Hey @thiagobustamante

I'm trying to generate swagger docs for some typescript that looks like:


export interface DesignJson {
  assemblyMethod: { [key: string]: AssemblyMethod };
  ...etc
}


export interface AssemblyMethod {
  id: string;
  name: string;
}

However, when it gets made into the swagger.yml file, it looks like this:

    DesignJson:
        description: ""
        properties:
            assemblyMethod:
                type: object
                properties: {}
                description: ""

Ideally it would look more like:

    DesignJson:
        description: ""
        properties:
            assemblyMethod:
                type: object
                properties: {}
                description: ""
                additionalProperties:
                    type: object
                    properties:
                        id:
                            type: string
                        name:
                            type: string

As per swagger specs image

https://swagger.io/docs/specification/data-models/dictionaries/

I see this code in typescript-rest-swagger, which I assume should be finding those additional properties:

function getModelTypeAdditionalProperties(node: UsableDeclaration) {
    if (node.kind === ts.SyntaxKind.InterfaceDeclaration) {
        const interfaceDeclaration = node as ts.InterfaceDeclaration;
        return interfaceDeclaration.members
            .filter(member => member.kind === ts.SyntaxKind.IndexSignature)
            .map((member: any) => {
                const indexSignatureDeclaration = member as ts.IndexSignatureDeclaration;

                const indexType = resolveType(indexSignatureDeclaration.parameters[0].type as ts.TypeNode);
                if (indexType.typeName !== 'string') {
                    throw new Error(`Only string indexers are supported. Found ${indexType.typeName}.`);
                }

                return {
                    description: '',
                    name: '',
                    required: true,
                    type: resolveType(indexSignatureDeclaration.type as ts.TypeNode)
                };
            });
    }

    return undefined;
}

but it must not be working 100%

Could you possibly take a look into this and let me know if I'm on the right track? Seems like the logic is there but it is just not detecting my TS interface as having additionalProperties.

Thanks! Thomas

tnrich avatar Jun 09 '20 19:06 tnrich

@tnrich have you figured that out ? I believe the method buildDefinitions in generator.ts is the one handling this. I'm trying to generate a swagger file with an additionalProperties: false but couldn't find a way yet.

rcrodrigues avatar Oct 21 '20 17:10 rcrodrigues

@rcrodrigues I haven't figured this one out still. Put it on hold for the time being. It would definitely still be nice to have a solution

tnrich avatar Oct 21 '20 17:10 tnrich