44import java .util .HashMap ;
55import java .util .HashSet ;
66import java .util .Map ;
7- import java .util .Optional ;
87import java .util .Set ;
98
109import com .fasterxml .jackson .databind .JsonNode ;
1110
12- import io .swagger .models .Model ;
13- import io .swagger .models .Operation ;
14- import io .swagger .models .Path ;
15- import io .swagger .models .Response ;
16- import io .swagger .models .Swagger ;
17- import io .swagger .models .parameters .BodyParameter ;
18- import io .swagger .models .parameters .Parameter ;
19- import io .swagger .parser .SwaggerParser ;
11+ import io .swagger .parser .OpenAPIParser ;
12+ import io .swagger .v3 .oas .models .OpenAPI ;
13+ import io .swagger .v3 .oas .models .Operation ;
14+ import io .swagger .v3 .oas .models .PathItem ;
15+ import io .swagger .v3 .oas .models .media .Schema ;
16+ import io .swagger .v3 .oas .models .responses .ApiResponse ;
17+ import io .swagger .v3 .parser .core .models .ParseOptions ;
18+ import io .swagger .v3 .parser .core .models .SwaggerParseResult ;
19+ import org .apache .commons .lang .*;
2020import lombok .Getter ;
2121import lombok .extern .slf4j .Slf4j ;
2222
2323@ Slf4j
2424public class BaseJsonSchemaGenerator {
2525
26+ public static final String APPLICATION_JSON = "application/json" ;
2627 @ Getter
2728 protected Map <String , JsonNode > generatedObjects = new HashMap <String , JsonNode >();
2829 protected static final String ORIGINAL_REF = "originalRef" ;
2930 protected static final String HTTP_JSON_SCHEMA_ORG_DRAFT_04_SCHEMA = "http://json-schema.org/draft-04/schema#" ;
3031 protected static final String $SCHEMA = "$schema" ;
31- protected static final String DEFINITIONS2 = "#/definitions /" ;
32+ protected static final String DEFINITIONS2 = "#/components/schemas /" ;
3233 protected static final String TITLE2 = "title" ;
3334 protected static final String ADDITIONAL_PROPERTIES = "additionalProperties" ;
34- protected static final String DEFINITIONS = "definitions" ;
3535 protected static final String MAX_ITEMS = "maxItems" ;
3636 protected static final String MIN_ITEMS = "minItems" ;
3737 protected static final String REQUIRED = "required" ;
@@ -41,43 +41,58 @@ public class BaseJsonSchemaGenerator {
4141 @ Getter
4242 protected Set <String > messageObjects = new HashSet <String >();
4343 @ Getter
44- private Map <String , Model > objectsDefinitions = new HashMap <String , Model >();
44+ private Map <String , Schema > objectsDefinitions = new HashMap <String , Schema >();
4545
4646
47- protected Swagger readFromInterface20 (File interfaceFile ) {
48-
49- Swagger swagger = new SwaggerParser ().read (interfaceFile .getAbsolutePath ());
50- objectsDefinitions = swagger .getDefinitions ();
51- for (Map .Entry <String , Path > entry : swagger .getPaths ().entrySet ()) {
47+ protected void readFromInterface (File interfaceFile ) {
48+ ParseOptions po = new ParseOptions ();
49+ po .setResolve (true );
50+ SwaggerParseResult result = new OpenAPIParser ().readLocation (interfaceFile .getAbsolutePath (),null ,po );
51+ OpenAPI swagger = result .getOpenAPI ();
52+ Validate .notNull (swagger ,"Error during parsing of interface file " +interfaceFile .getAbsolutePath ());
53+ objectsDefinitions = swagger .getComponents ().getSchemas ();
54+ for (Map .Entry <String , PathItem > entry : swagger .getPaths ().entrySet ()) {
5255 String k = entry .getKey ();
53- Path v = entry .getValue ();
54- log .info (k + "=" + v );
55- for (Operation op : v .getOperations ()) {
56- log .info ("Operation={}" , op .getOperationId ());
57- Optional <Parameter > body = op .getParameters ().stream ().filter (c -> "body" .equalsIgnoreCase (c .getIn ()))
58- .findFirst ();
59- if (body .isPresent ()) {
60- BodyParameter bp = (BodyParameter ) body .get ();
61- log .info ("Request schema={}" , bp .getSchema ().getReference ());
62- messageObjects .add (bp .getSchema ().getReference ());
56+ PathItem v = entry .getValue ();
57+ analyzeOperation (v );
58+ }
59+ }
6360
64- }
65- for (Map .Entry <String , Response > entry2 : op .getResponses ().entrySet ()) {
66- String rk = entry2 .getKey ();
67- Response r = entry2 .getValue ();
68- if (r .getResponseSchema () != null ) {
69- if (r .getResponseSchema ().getReference ()!=null ) {
70- messageObjects .add (r .getResponseSchema ().getReference ());
71- log .info ("code={} responseSchema={}" , rk , r .getResponseSchema ().getReference ());
61+ private void analyzeOperation (PathItem v ) {
62+ for (Operation op : v .readOperations ()) {
63+ log .info ("Operation={}" , op .getOperationId ());
64+ findRequestBodySchema (op , messageObjects );
65+ for (String key : op .getResponses ().keySet ()){
66+ ApiResponse r = op .getResponses ().get (key );
67+ if (r .getContent ()!=null ) {
68+ if (r .getContent ().get (APPLICATION_JSON ) != null ) {
69+ if (r .getContent ().get (APPLICATION_JSON ).getSchema ().get$ref () != null ) {
70+ log .info ("code={} responseSchema={}" , key , r .getContent ().get (APPLICATION_JSON ).getSchema ().get$ref ());
71+ messageObjects .add (r .getContent ().get (APPLICATION_JSON ).getSchema ().get$ref ());
7272 } else {
73- log .warn ("code={} response schema is not a referenced definition! type={}" ,rk , r . getResponseSchema ( ).getClass ());
73+ log .warn ("code={} response schema is not a referenced definition! type={}" , key , r . getContent (). get ( "application/json" ).getClass ());
7474 }
75-
7675 }
7776 }
7877 }
78+ }
79+ }
7980
81+ private void findRequestBodySchema (Operation op , Set <String > messageObjects ) {
82+ if (op .getRequestBody ()!=null ) {
83+ if (op .getRequestBody ().getContent ().get (APPLICATION_JSON )!=null ) {
84+ Schema sc = op .getRequestBody ().getContent ().get (APPLICATION_JSON ).getSchema ();
85+ if (sc != null ) {
86+ log .info ("Request schema={}" , sc .get$ref ());
87+ if (sc .get$ref ()!=null ) {
88+ messageObjects .add (sc .get$ref ());
89+ } else {
90+ log .warn ("Request schema is not a referenced definition!" );
91+ }
92+ }
93+ } else {
94+ log .info ("No application body of type 'application/json' found for operation {}" ,op .getOperationId ());
95+ }
8096 }
81- return swagger ;
8297 }
8398}
0 commit comments