iconicon
BBBlllooogggPPPooorrrtttfffooollliiiooo
    OpenAPI

    ๐Ÿ“— OpenAPI Generator ์‚ฌ์šฉ๋ฒ•

    2026๋…„ 01์›” 07์ผ

    On this page

    • ์„ค์น˜ ๋ฐ ๊ตฌ์„ฑ
    • ์„ค์น˜

    On this page
    • ์„ค์น˜ ๋ฐ ๊ตฌ์„ฑ
    • ์„ค์น˜

    ์„ค์น˜ ๋ฐ ๊ตฌ์„ฑ


    • ํ•ด๋‹น ๋ฌธ์„œ๋Š” SON50์— ์ ์šฉ๋œ openapi-generator๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

    • OpenAPI Generator์€ Swagger UI์˜ Json, yaml ๋ฌธ์„œ๋ฅผ ์ž๋™์œผ๋กœ Front-End ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.

    ์„ค์น˜


    Java jdk๊ฐ€ ๋ฏธ๋ฆฌ ์„ค์น˜๋œ ์ƒํƒœ์—์„œ ์ง„ํ–‰ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. Microsoft Build of OpenJDK ๋‹ค์šด๋กœ๋“œ

    ์„ค์น˜ ํ›„, java ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ธํŒ…๋„ ํ•„์š”ํ•˜๋‹ค. [Mac OS] ๋งฅ๋ถ JAVA ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

    1. openAPI generator ์„ค์น˜
    npm install @openapitools/openapi-generator-cli -D
    
    1. nodejs ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ ( update_api.js )
    import { execSync } from 'child_process';
    import fs from 'fs';
    
    const swaggerUrl = {
      maestro: 'http://10.78.0.151:12190/v3/api-docs/maestro',
    }; // ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
    
    const saveJsonFile = {
      maestro: './maestro.json',
    };
    
    const outPath = {
      maestro: '../src/api/maestro',
    }; // ๋ณ€ํ™˜ ์œ„์น˜
    
    const configPath = './openapi-generator-config.json';
    
    // ๋ช…๋ น ์‹คํ–‰
    Object.keys(swaggerUrl).forEach(async key => {
      await fetch(swaggerUrl[key], { method: 'get' })
        .then(res => res.json())
        .then(json => {
          Object.keys(json.paths).forEach(path => {
            Object.keys(json.paths[path]).forEach(method => {
              if (json.paths[path][method].operationId) {
                delete json.paths[path][method].operationId;
              }
            });
          });
    
          // ์ƒˆ๋กœ์šด JSON ํŒŒ์ผ๋กœ ์ €์žฅ
          fs.writeFile(saveJsonFile[key], JSON.stringify(json, null, 2), 'utf8', () => {
            execSync(
              `openapi-generator-cli generate -i ${saveJsonFile[key]} \
             -g typescript-axios -c ${configPath} \
             -o ${outPath[key]} --skip-validate-spec`,
              {
                stdio: 'inherit',
              },
            );
    
            if (fs.existsSync(saveJsonFile[key])) {
              fs.unlinkSync(saveJsonFile[key]);
            }
          });
        });
    });
    
    1. config ์„ค์ • ( openapi-generator-config.json )
    { 
    	"modelPackage": "types", // ์ƒ์„ฑ๋œ ํƒ€์ž… ๊ฒฝ๋กœ 
    	"apiPackage": "api", // ์ƒ์„ฑ๋œ API ํด๋ž˜์Šค ๊ฒฝ๋กœ 
    	"supportsES6": true, // ES6 ํ˜•์‹ 
    	"withSeparateModelsAndApi": true // ๋ชจ๋ธ, API๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์ƒ์„ฑ 
    	"generateOperationIdBasedOnUrl": true // URL๋กœ ํ•จ์ˆ˜๋ช… ์ƒ์„ฑ 
    }
    
    1. package.json ์— ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ ๋ฐ ์‹คํ–‰
    {
      "scripts": {
        "updateApi": "cd ./swagger && node ./update_api.js"
      }
    }
    
    npm run updateApi