swagger-api / swagger-api/swagger-codegen

[JAVA] [Resttemplate Client] Empty authNames in generated API classes

Open
#8,501 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Hi everybody. I have an strange behaviour after generating code with resttemplate client library. Our swagger document has securityDefinitions


securityDefinitions:

  default:

    type: oauth2

    authorizationUrl: 'https://beholdertest.minessotta.es:443/authorize'

    flow: implicit

    scopes: {}

After running the generator, the api classes contains the authnames arrays empty.

 String[] authNames = new String[] {  };

I have modified the method

public CodegenOperation fromOperation(String path,String httpMethod,Operation operation,Map<String, Model> definitions,Swagger swagger)

from DefaultCodegen.java

in order to include the neccesary information (according to the mustache template)


List<CodegenSecurity> allMethods = new ArrayList<CodegenSecurity>();
        Map<String,SecuritySchemeDefinition> secDefinitions = swagger.getSecurityDefinitions();
        
        if (secDefinitions!=null && secDefinitions.size()>0){
        	for (Entry<String, SecuritySchemeDefinition>current:secDefinitions.entrySet()){
        		CodegenSecurity newSecurity=fromSecurityDefinition(current.getKey(),current.getValue());
        		allMethods.add(newSecurity);
        	}
        }
        op.authMethods=allMethods;


 private CodegenSecurity fromSecurityDefinition(String key, SecuritySchemeDefinition value) {
    	CodegenSecurity sec=new CodegenSecurity();
    	sec.name=key;
		return sec;
	}

Swagger-codegen version

2.3.1

Swagger declaration file content or url

swagger: '2.0'
info:
  description: 'Pasarela '
  version: 1.0.0
  title: Minessotta API transformer
  termsOfService: 'http://en.wikipedia.org/wiki/Terms_of_service'
  contact:
    name: archit
    url: 'http://aaa.com'
    email: archit@minessotta.es
  license:
    name: Apache License Version 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: 'beholdertest.minessotta.es:443'
paths:
  /sms:
    post:
      tags:
        - Servicios SMS
      summary: Recuperar estado de mensaje
      description: 'Recupera el estado de un mensaje, mediante el idmensaje'
      operationId: getMessageUsingPOST
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: estado
          description: estado
          required: true
          schema:
            $ref: '#/definitions/EstadoMensaje'
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/SMSMinessotta_MT_ConsultaEstado_Out'
        '201':
          description: Created
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      x-auth-type: Application & Application User
      x-throttling-tier: Unlimited
    put:
      tags:
        - Servicios SMS
      summary: Enviar mensaje
      description: Envia un mensaje
      operationId: sendMessageUsingPUT
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: estadoBody
          description: estadoBody
          required: true
          schema:
            $ref: '#/definitions/EnvioMensaje'
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/SMSMinessotta_MT_EnvioMensaje_Out'
        '201':
          description: Created
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      x-auth-type: Application & Application User
      x-throttling-tier: Unlimited
definitions:
  EnvioMensaje:
    type: object
    required:
      - aplicacion
      - cliente
      - destinatarios
      - mensaje
      - password
    properties:
      aplicacion:
        type: string
        description: Id Aplicacion
      cliente:
        type: string
        description: Id Cliente
      destinatarios:
        type: array
        description: Lista de telefonos
        items:
          type: string
      mensaje:
        type: string
        description: Texto del mensaje
      password:
        type: string
        description: Password
    description: Datos de envio
  EstadoMensaje:
    type: object
    required:
      - aplicacion
      - cliente
      - idmensaje
      - password
    properties:
      aplicacion:
        type: string
        description: Id Aplicacion
      cliente:
        type: string
        description: Id Cliente
      idmensaje:
        type: string
        description: Id mensaje
      password:
        type: string
        description: Password
    description: Estado del mensaje
  SMSMinessotta_MT_ConsultaEstado_Out:
    type: object
    properties:
      error:
        type: string
      idmensaje:
        type: string
      modo:
        type: string
  SMSMinessotta_MT_EnvioMensaje_Out:
    type: object
    properties:
      error:
        type: string
      identificador:
        type: string
      identificadorMT:
        type: string
      modo:
        type: string
      tiempo:
        type: string
securityDefinitions:
  default:
    type: oauth2
    authorizationUrl: 'https://beholdertest.minessotta.es:443/authorize'
    flow: implicit
    scopes: {}
basePath: /minessotta-utils/1.0.0
schemes:
  - https
  - http

Command line used for generation

java -jar swagger-codegen-2.3.1.jar generate -i c:\temp\av.json --api-package es.minessotta.client.api --model-package es.minessotta.client.model --invoker-package es.minessotta.client.invoker --group-id es.minessotta.av --artifact-id client-for-av --artifact-version 0.0.1-SNAPSHOT -l java --library resttemplate -o c:\temp\client-for-av

Steps to reproduce

Generate code

Related issues/PRs
Suggest a fix/enhancement

I think the CodegenOperation object does not have the infor regarding security definitions. So, that might be the reason for generate and empty authNames array.

Thanks a lot in advance

Kind Regards

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with DefaultCodegen.java's fromOperation method and the RestTemplate mustache template that consumes authNames. Reproduce the issue with the supplied Swagger document and generation command, then verify that generated API classes populate authNames from the declared securityDefinitions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.