OpenAPITools / OpenAPITools/openapi-generator

[BUG] Description Security not working with inputSpecRootDirectory on Java client generator Maven

Open
#19,894 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

When using the configuration inputSpecDirectory, a _merged_spec.yaml file is created and used to generate the Java client. The securityschemas of the merged files are ignored at this point and the client will be generated without any security authorization which results in following:

    public ApiClient() {
        init();
        initHttpClient();

        // Setup authentications (key: authentication name, value: authentication).
        // Prevent the authentications from being modified.
        authentications = Collections.unmodifiableMap(authentications);
    }

expected would be:

    public ApiClient() {
        init();
        initHttpClient();

        // Setup authentications (key: authentication name, value: authentication).
        authentications.put("basicAuth", new HttpBasicAuth());
        // Prevent the authentications from being modified.
        authentications = Collections.unmodifiableMap(authentications);
    }
openapi-generator version

7.8.0

OpenAPI declaration file content or url

If you post the code inline, please wrap it with

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: "http: //petstore.swagger.io/v1"
paths:
  /pets/{petId}:
    get:
      security:
      - basicAuth: [
          ]
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      maxItems: 100
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
Generation Details
<inputSpecRootDirectory>
${project.basedir}/src/main/resources/api-definitions
</inputSpecRootDirectory>
<!-- <skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged> -->
<generatorName>java</generatorName>
<library>okhttp-gson</library>
<generateApis>true</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<generateModels>true</generateModels>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<skipValidateSpec>true</skipValidateSpec>
<generateSupportingFiles>true</generateSupportingFiles>
<apiPackage>your.app.api</apiPackage>
<modelPackage>your.app.model</modelPackage>
<output>${project.build.directory}/generated-sources</output>
<configOptions>
    <useJakartaEe>true</useJakartaEe>
    <oas3>true</oas3>
    <useSpringController>true</useSpringController>
    <useSpringfox>false</useSpringfox>

    <sourceFolder>main/java</sourceFolder>
    <!-- Use modern java8 date/time api -->
    <dateLibrary>java8</dateLibrary>
    <java8>true</java8>
    <!-- Do not include any validation with the client -->
    <useBeanValidation>false</useBeanValidation>
    <performBeanValidation>false</performBeanValidation>
    <interfaceOnly>false</interfaceOnly>
    <useOptional>false</useOptional>
    <!-- Make the models serializable -->
    <serializableModel>true</serializableModel>
</configOptions>
Steps to reproduce
  • add openapi-generator-maven-plugin to the project
  • add a folder with definition in it
  • use inputSpecRootDirectory pointing to that directory
  • generate client
Suggest a fix

merge the securitySchemes of the files together and add them to the _merged_spec.yaml

Tested it out by adding it manually to the _merged_spec.yaml and it worked:

components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic

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

Start at the Maven plugin path handling inputSpecRootDirectory and the generation of _merged_spec.yaml. Reproduce the Java client generation with split definitions and inspect whether components.securitySchemes are preserved in the merged specification. Done means the generated ApiClient includes the declared basicAuth configuration when the source definitions are merged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.