Giter VIP home page Giter VIP logo

openapi2puml's Introduction

OpenAPI2Puml

Build Status codecov Gitpod ready-to-code

OpenApi to Plant UML conversion tool generates UML Class Diagrams from an Open API definition.

This is a fork of the original project Swagger2puml (https://github.com/kicksolutions/swagger2puml) which seems to have been dormant for some time.

The original developers of Swagger2puml are:

This project is based on Maven. Following are modules we currently have

  • openapi2puml-core

Following are the tools which this project internally uses:

How does it work

  • Input: Openapi2Puml parses the swagger definition from input using Swagger Parser
  • Transform: The swagger definition is built into an object model
  • Output: The object model is transformed into a Plant UML file using a Mustache template. Optionally a .svg image can be generated also.

openapi2puml-core:

This utility takes OpenAPI Yaml or JSON as input generates swagger.puml and swagger.svg files as output.

Below is the Sample Class Diagram generated by the application. To see the generated PUML file, please click here

Swagger-Class-Diagram-Sample

Building:

mvn package

The jar is built with dependencies and placed in the root of the project.

Usage:

java -jar openapi2puml.jar [options]

-i {Path of Swagger Definition (Can be either Yaml or json)}
-o {Target location where Puml File and Image should generated}
-generateDefinitionModelOnly {true/flase Defult False (Optional)}
-includeCardinality {true/flase Defult true (Optional)}
-generateSvg true/false; Default=true

Dockerized version of OpenAPI2Puml

It is also to use a docker container to run the tool. Simply mount a directory and add command line options by using docker run, e.g.

# Run a docker container
$ sudo docker run --rm -it --name openapi2puml \
    -v $PWD/examples:/specs \
    openapi2puml/openapi2puml -i /specs/swagger.yaml -o /specs

A dockerhub repo exists here: https://hub.docker.com/r/openapi2puml/openapi2puml

To build and run a local copy, do the following:

# Obtain base image for build
$ sudo docker pull maven:3-jdk-11  # needed since docker-hub pull restrictions

# Build the image from the project
$ sudo docker build -t openapi2puml .

# Run a docker container
$ sudo docker run --rm -it --name openapi2puml \
    -v $PWD/examples:/specs \
    openapi2puml -i /specs/swagger.yaml -o /specs

Running in GitPod

You can directly run this code in GitPod by clicking this link and Running

and then using this command line:

java -cp /workspace/openapi2puml/openapi2puml-core/target/dependency/*:/workspace/openapi2puml/openapi2puml-core/target/openapi-plantuml-core-0.0.1-SNAPSHOT.jar org.openapi2puml.openapi.OpenApi2PlantUML -i /workspace/openapi2puml/examples/swagger.yaml -o /workspace/openapi2puml/examples

License

Apacahe 2.0

openapi2puml's People

Contributors

astrodex2020 avatar darrenc avatar dependabot[bot] avatar liam-deacon avatar manishabardiya avatar santoshm1188 avatar xpomul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

openapi2puml's Issues

Multiple types of Error Responses result in an invalid PlantUML diagram

If the swagger specification contains multiple types of error response, the generated PlantUML diagram contains invalid syntax.

For example - below the HTTP 400 response uses an "ErrorMessage" structure and the HTTP 404 response uses a "WarningMessage" structure:

  '/cat/{catId}':
    get:
      tags:
        - cat
      summary: Find cat by ID
      description: Returns a single cat
      operationId: getCatById
      produces:
        - application/xml
        - application/json
      parameters:
        - name: catId
          in: path
          description: ID of cat to return
          required: true
          type: integer
          format: int64
      responses:
        '200':
          description: successful operation
          schema:
            $ref: '#/definitions/Cat'
        '400':
          description: Invalid ID supplied
          schema:
            $ref: '#/definitions/ErrorMessage'
        '404':
          description: Cat not found
          schema:
            $ref: '#/definitions/WarningMessage'

The resulting PlantUML contains this definition for the relationship which is not valid.

CatApi --> ErrorMessage,WarningMessage

SVG image generation creates multiple files

When running unit tests or from the jar, there are multiple svg file copies created.
I suspect this comes from just getting all created files so the solution may be to get just a specific file instead.

Code Style reformatting and cleanup

There is a mix of styles in the project with different spacing etc.
There are also empty comments.

This issue will be to apply consistent style conventions to the code and commenting.

Invalid OpenAPI definition

My YAML file is correct but this plugin does not accept it:
Error:

Invalid OpenAPI definition
[pointer: "",
message: object instance has properties which are not allowed by the schema: ["components","openapi","servers"]
, pointer: "",
message: object has missing required properties (["swagger"])
]

build jar with deps

The current maven build and usage descriptions are somewhat misleading.
The jar built by default does not include dependencies and so the usage instructions as given in the readme do not work.

A solution is already provided on the original Swagger2Puml repository (kicksolutions/swagger2puml#40)
submitted by @dgileadi

@dgileadi - please feel free to submit a PR here with your fix

Interfaces with names containing "-" break puml

The definition section is fine, but the "graphic" section with the arrows is broken as the names are not quoted

e.g.:
Discovery-document-controllerApi --> IdpJwksDocument
should be
"Discovery-document-controllerApi" --> IdpJwksDocument

Change method of SVG/PNG image generation

The image generation for SVG is done by invoking the main method of the Run class from the PlantUML library.

This seems to not work well with JUnit tests and is a strange approach to the image file generation.

This enhancement should follow the example from the PlantUml site - http://plantuml.com/api
It uses the class SourceFileReader to read the generated PlantUML file and to generate the resulting image file.

Use standard logger and make information more relevant

The current code uses the inbuilt java.util logger classes and is quite verbose without really adding anything useful - i.e. entry and exit of methods is logged even if successful, no real information is logged if an error occurs.

The enhancement would be to:

  • Move to either slf4j or log4j2 (or another logging system) to allow a finer granularity of logging
  • Refactor the logging calls to remove some calls and add some useful summary information - e.g. list all the operations parsed from Swagger etc.

Paths in Swagger create duplicate Interfaces with just one method in each

When a path has multiple operations, instead of this mapping to a single Interface with multiple methods, an Interface item is created with one single method for each operation.
These Interface items are added to the list of interface items and this is used by the mustache template to build the final uml output.

PlantUML viewers seem to be smart enough to merge these multiple definitions into one but it would be better to organize the data model objects better.

Support for open api v3

Hi, notice that it cannot convert api specs from v2 onwards. Will you support for open api v3 definition?

NPE when using multilevel inheritance - from original swagger2puml

This is a copy of an issue opened on the original swagger2puml repository which was never fixed.
kicksolutions/swagger2puml#41

The issue was opened and a patch solution proposed by @stroisi (https://github.com/stroisi)

I also took the patch solution, added some simple unit tests and opened a PR on the original repository which has never been merged.

Original Issue description below:

When processing a yaml with definitions that include multiple levels of inheritance an NPE is thrown.

Example model:
`

  GrandParentObj:
    discriminator: objType
    required:
      - objType
    properties:
      prop1:
        type: string
      objType:
        type: string

  ParentObj:
    allOf:
      - $ref: '#/definitions/GrandParentObj'
      - type: object
        properties:
          prop2:
            type: string
        
  ChildObj1:
    allOf:
      - $ref: '#/definitions/ParentObj'
      - type: object
        properties:
          prop3:
            type: string`

NPE Stacktrace:
`

Caused by: java.lang.NullPointerException
    at java.util.HashMap.putMapEntries (HashMap.java:501)
    at java.util.HashMap.putAll (HashMap.java:785)
    at com.kicksolutions.swagger.plantuml.PlantUMLCodegen.getClassMembers (PlantUMLCodegen.java:745)
    at com.kicksolutions.swagger.plantuml.PlantUMLCodegen.getClassMembers (PlantUMLCodegen.java:693)
    at com.kicksolutions.swagger.plantuml.PlantUMLCodegen.processSwaggerModels (PlantUMLCodegen.java:577)
    at com.kicksolutions.swagger.plantuml.PlantUMLCodegen.preprocessSwagger (PlantUMLCodegen.java:120)
    at com.kicksolutions.swagger.plantuml.PlantUMLCodegen.generatePuml (PlantUMLCodegen.java:83)
    at com.kicksolutions.swagger.plantuml.PlantUMLGenerator.transformSwagger2Puml (PlantUMLGenerator.java:43)
    at com.kicksolutions.swagger.Swagger2PlantUML.process (Swagger2PlantUML.java:71)
    at com.kicksolutions.swagger.Swagger2PlantUML.init (Swagger2PlantUML.java:55)
    at com.kicksolutions.swagger.Swagger2PlantUML.main (Swagger2PlantUML.java:37)`

It looks like when the getClassMembers(ComposedModel) method loops over the "AllOf" list of the target model to resolve the parent model's properties, that if the parent is itself the child of another type that it's getProperties() method returns null, causing the NPE when we try to put them into the child's Map of properties.

I'm attaching a patch with a proposed fix.

There is a secondary issue, also addressed in the patch, which is that even with the NPE fixed the code wasn't correctly resolving the full list of properties from the descendant types.

In the example above, the UML generated for "ChildObj1" looked like:
`

class ChildObj1 {

  • prop3 :String
    }`

With the patch applied we recurse to get properties from the descendants, and the output includes prop1 & prop2 from the parent & grandparent types:
`

class ChildObj1 {

  • prop3 :String
  • prop2:String
  • prop1:String
    }`

Original Zip file with patch:
multiple-inheritence-npe-patch.zip

Fat jar build contains unecessary dependencies

The fat jar generated with dependencies contains unneccessary jar files (e.g. mockito) that could be left out to reduce the size of the final jar.

Correct use of a scope for each dependency in the Maven pom files should be a solution.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.