diff --git a/src/main/jastadd/OpenAPISpecification.relast b/src/main/jastadd/OpenAPISpecification.relast index 7222c7a0313ad104c4588347cd1deba77c0b2275..13fa53e462621bf09f0b9458854ba4a7c7ece110 100644 --- a/src/main/jastadd/OpenAPISpecification.relast +++ b/src/main/jastadd/OpenAPISpecification.relast @@ -25,7 +25,7 @@ ComponentsObject ::= S:SchemaTuple* R:ResponseTuple* P:ParameterTuple* E:Example SchemaTuple ::= <Key> O:SchemaOb; ResponseTuple ::= <Key> O:ResponseOb; ParameterTuple ::= <Key> O:ParameterOb; -ExampleTuple ::= <Key> O:ExampleObject; +ExampleTuple ::= <Key> O:ExampleOb; RequestBodyTuple ::= <Key> O:RequestBodyOb; HeaderTuple ::= <Key> O:HeaderOb; SecuritySchemeTuple ::= <Key> O:SecuritySchemeOb; @@ -97,7 +97,10 @@ Extension ::= <Key> <Value:Object>; Expression ::= <Name> P:PathItemOb; //Example Object -ExampleObject ::= <Summary> <Description> <Value:Object> <ExternalValue> Ex:Extension*; +abstract ExampleOb; +ExampleReference : ExampleOb ::= <Ref> <Summary> <Description>; +rel ExampleReference.r -> ExampleObject; +ExampleObject : ExampleOb ::= <Summary> <Description> <Value:Object> <ExternalValue> Ex:Extension*; //Link Object abstract LinkOb; diff --git a/src/main/jastadd/Parser.jrag b/src/main/jastadd/Parser.jrag index ccb1187aa1f754b9eba67c90602f8a11f44d65e1..03d01845f6528696131bb586ca2f1fa95af03142 100644 --- a/src/main/jastadd/Parser.jrag +++ b/src/main/jastadd/Parser.jrag @@ -542,10 +542,21 @@ public static CallbackOb CallbackOb.parseCallback(Callback callback) { return callbackObject; } - -public static ExampleObject ExampleObject.parseExample(Example example){ +public static ExampleOb ExampleOb.parseExample(Example example){ ExampleObject exampleObject = new ExampleObject(); + if (example.get$ref() != null) { + ExampleReference e = new ExampleReference(); + e.setRef(example.get$ref()); + + /* 3.1.0 features + if (example.getSummary() != null) ... + if (example.getDescription() != null) ... + */ + + return e; + } else { + if (example.getSummary() != null) exampleObject.setSummary(example.getSummary()); if (example.getDescription() != null) @@ -558,6 +569,7 @@ public static ExampleObject ExampleObject.parseExample(Example example){ for (String key : example.getExtensions().keySet()) exampleObject.addEx(new Extension(key, example.getExtensions().get(key))); } + } return exampleObject; } @@ -791,14 +803,14 @@ public static DiscriminatorObject DiscriminatorObject.parseDiscriminator (Discri DiscriminatorObject discriminatorObject = new DiscriminatorObject(); if (discriminator.getPropertyName() != null) - discriminatorObject.setPropertyName(discriminator.getPropertyName()); + discriminatorObject.setPropertyName(discriminator.getPropertyName()); if (discriminator.getMapping() != null) { - MappingTuple mapping = new MappingTuple(); - for (String key : discriminator.getMapping().keySet()) { - mapping.setKey(key); - mapping.setValue(discriminator.getMapping().get(key)); - discriminatorObject.addM(mapping); - } + for (String key : discriminator.getMapping().keySet()) { + MappingTuple mapping = new MappingTuple(); + mapping.setKey(key); + mapping.setValue(discriminator.getMapping().get(key)); + discriminatorObject.addM(mapping); + } } return discriminatorObject; diff --git a/src/main/jastadd/ReverseParser.jrag b/src/main/jastadd/ReverseParser.jrag index 224126835dbb8e3fbbbcbdb72a34d6fe33e08575..95bb3a9af36d1aa2b95bdaf0dffc809d976f0528 100644 --- a/src/main/jastadd/ReverseParser.jrag +++ b/src/main/jastadd/ReverseParser.jrag @@ -191,7 +191,7 @@ aspect ReverseParser{ if (componentsObject.getNumE() != 0) { Map<String, Example> exampleMap = new HashMap<>(); for (ExampleTuple t : componentsObject.getEList()) - exampleMap.put(t.getKey(), ExampleObject.reverseExample(t.getO())); + exampleMap.put(t.getKey(), t.getO().reverseExample(t.getO())); components.setExamples(exampleMap); } if (componentsObject.getNumRb() != 0) { @@ -435,7 +435,7 @@ aspect ReverseParser{ if (p.getNumE() != 0) { Map<String, Example> exampleMap = new HashMap<>(); for (ExampleTuple t : p.getEList()) - exampleMap.put(t.getKey(), ExampleObject.reverseExample(t.getO())); + exampleMap.put(t.getKey(), t.getO().reverseExample(t.getO())); parameter.setExamples(exampleMap); } if (p.getNumC() != 0) { @@ -498,7 +498,7 @@ aspect ReverseParser{ if (mediaTypeObject.getNumE() != 0) { Map<String, Example> exampleMap = new HashMap<>(); for (ExampleTuple t : mediaTypeObject.getEList()) - exampleMap.put(t.getKey(), ExampleObject.reverseExample(t.getO())); + exampleMap.put(t.getKey(), t.getO().reverseExample(t.getO())); mediaType.setExamples(exampleMap); } if (mediaTypeObject.getNumEn() != 0) { @@ -554,7 +554,7 @@ aspect ReverseParser{ ApiResponse response = new ApiResponse(); ResponseObject r = (ResponseObject) responseOb; - if (!r.getDescription().isEmpty()) + if (r.getDescription() != null) response.setDescription(r.getDescription()); if (r.getNumH() != 0) { Map<String, Header> headers = new HashMap<>(); @@ -603,26 +603,37 @@ aspect ReverseParser{ return callback; } - public static Example ExampleObject.reverseExample(ExampleObject exampleObject){ + syn Example ExampleOb.reverseExample(ExampleOb exampleOb); +eq ExampleReference.reverseExample(ExampleOb exampleOb){ Example example = new Example(); + ExampleReference e = (ExampleReference) exampleOb; - if (!exampleObject.getSummary().isEmpty()) - example.setSummary(exampleObject.getSummary()); - if (!exampleObject.getDescription().isEmpty()) - example.setDescription(exampleObject.getDescription()); - if (exampleObject.getValue() != null) - example.setValue(exampleObject.getValue()); - if (!exampleObject.getExternalValue().isEmpty()) - example.setExternalValue(exampleObject.getExternalValue()); - if (exampleObject.getNumEx() != 0) { - Map<String, Object> extension = new HashMap<>(); - for (Extension e : exampleObject.getExList()) - extension.put(e.getKey(), e.getValue()); - example.setExtensions(extension); - } + if(!e.getRef().isEmpty()) + example.set$ref(e.getRef()); - return example; - } + return example; +} + eq ExampleObject.reverseExample(ExampleOb exampleOb){ + Example example = new Example(); + ExampleObject exampleObject = (ExampleObject) exampleOb; + + if (!exampleObject.getSummary().isEmpty()) + example.setSummary(exampleObject.getSummary()); + if (!exampleObject.getDescription().isEmpty()) + example.setDescription(exampleObject.getDescription()); + if (exampleObject.getValue() != null) + example.setValue(exampleObject.getValue()); + if (!exampleObject.getExternalValue().isEmpty()) + example.setExternalValue(exampleObject.getExternalValue()); + if (exampleObject.getNumEx() != 0) { + Map<String, Object> extension = new HashMap<>(); + for (Extension e : exampleObject.getExList()) + extension.put(e.getKey(), e.getValue()); + example.setExtensions(extension); + } + + return example; + } syn io.swagger.v3.oas.models.links.Link LinkOb.reverseLink(LinkOb linkOb); eq LinkReference.reverseLink(LinkOb linkOb){ @@ -693,7 +704,7 @@ aspect ReverseParser{ if (h.getNumE() != 0) { Map<String, Example> examples = new HashMap<>(); for (ExampleTuple t : h.getEList()) - examples.put(t.getKey(), ExampleObject.reverseExample(t.getO())); + examples.put(t.getKey(), t.getO().reverseExample(t.getO())); header.setExample(examples); } if (h.getNumC() != 0) { diff --git a/src/main/resources/3.1/siblings31.yaml b/src/main/resources/3.1/siblings31.yaml deleted file mode 100644 index be867e2b10d9da38fc4bc5e2582be42d2e0ca4a3..0000000000000000000000000000000000000000 --- a/src/main/resources/3.1/siblings31.yaml +++ /dev/null @@ -1,46 +0,0 @@ -openapi: "3.1.0" -info: - version: 1.0.0 - title: Swagger Petstore -paths: - /pets: - $ref: "#/components/pathItems/Pets" - summary: List all pets - description: description - /pets/{petId}: - get: - summary: Info for a specific pet - operationId: showPetById - security: - - apiKey: [ ] - tags: - - pets - parameters: - - $ref: "#/components/parameters/Pets" - description: The id of the pet to retrieve - responses: - "200": - $ref: "#/components/responses/Pets" - description: Expected response to a valid request - /pets/requestBody: - post: - description: It gets pets - responses: - '200': - description: successful operation - headers: - X-Rate-Limit: - $ref: "#/components/headers/Pets" - description: header sibling description - links: - userRepository: - $ref: '#/components/links/userRepository' - description: link sibling description - requestBody: - $ref: "#/components/requestBodies/Pets" - description: request body description -components: - securitySchemes: - api_key: - $ref: "#/components/securitySchemes/security" - description: This is another description diff --git a/src/test/java/openapi/OpenAPIMain_test.java b/src/test/java/openapi/OpenAPIMain_test.java index 25928f7aa171e0c54f216d412427aa86964cb69d..8f7a6fe67c9f404018d2aa5565ddcc88ab220071 100644 --- a/src/test/java/openapi/OpenAPIMain_test.java +++ b/src/test/java/openapi/OpenAPIMain_test.java @@ -38,74 +38,12 @@ public class OpenAPIMain_test { File genDirectory = new File(genDir); File[] contents; - File resource = new File("./src/main/resources/3.0"); + File resource = new File("./src/main/resources"); - for (File file : resource.listFiles()) - filenames.add(file.getName()); - - for (String file : filenames) { - String writerName = genDir + file; - writerName = writerName.substring(0, writerName.length() - 5); - FileWriter expectedWriter = new FileWriter(writerName + "-expected.json"); - FileWriter actualWriter = new FileWriter(writerName + "-actual.json"); - - // parsed openAPI object with swagger-parser - SwaggerParseResult result = new OpenAPIParser().readLocation(resource.getPath() + "/" + file, null, null); - POJOOpenAPI = result.getOpenAPI(); - System.out.println("Loading expression DSL file '" + file + "'."); - - - // validation of OpenAPI in POJO - JsonNode expectedNode = mapper.readTree(Json.mapper().writeValueAsString(POJOOpenAPI)); - validation = new OpenAPIV3Parser().readContents(expectedNode.toString()).getMessages(); - if ( validation.size() != 0 ) { - System.out.println("validation failed!"); - for ( String s : validation ) - System.out.println(s); - } - else - System.out.println("validated!"); - - // save expected object - expectedWriter.write(expectedNode.toPrettyString()); - expectedWriter.close(); - - // OpenAPI in POJO to OpenAPI in JastAdd - jastAddObject = OpenAPIObject.parseOpenAPI(POJOOpenAPI); - - // OpenAPI in JastAdd to OpenAPI in POJO - OpenAPI transformedAPI = OpenAPIObject.reverseOpenAPI(jastAddObject); - - // validation of transferred OpenAPI - JsonNode actualNode = mapper.readTree(Json.mapper().writeValueAsString(transformedAPI)); - validation = new OpenAPIV3Parser().readContents(actualNode.toString()).getMessages(); - if ( validation.size() != 0 ) { - System.out.println("validation failed!"); - for ( String s : validation ) - System.out.println(s); - } - else - System.out.println("validated"); - - // save generated object - actualWriter.write(actualNode.toPrettyString()); - actualWriter.close(); - - // compare if api (source object) is equivalent to api3 (generated object) - compareJson(expectedNode, actualNode, Paths.get(file)); - } - - // clean all generated jsons - /* - contents = genDirectory.listFiles(); - if (contents != null) { - for (File file : contents) - file.delete(); - } - */ + recursiveTest(resource); } - protected void compareJson(JsonNode expectedNode, JsonNode actualNode, Path path) throws IOException { + protected static void compareJson(JsonNode expectedNode, JsonNode actualNode, Path path) throws IOException { JsonNode diff = JsonDiff.asJson(expectedNode, actualNode); String pathNode; String result = ""; @@ -116,6 +54,8 @@ public class OpenAPIMain_test { for (String s : pathNode.split("/")) { if (s.contains(".")) pathNode = pathNode.replace(s, "['" + s + "']"); + else if (s.contains(" ")) + pathNode = pathNode.replace(s, "['" + s + "']"); } pathNode = pathNode .replace("/", ".") @@ -129,8 +69,11 @@ public class OpenAPIMain_test { } pathNode = result.substring(0, result.length()-1); + //System.out.println(JsonPath.parse(expectedNode.toString()).read(pathNode, String.class)); + //System.out.println(JsonPath.parse(actualNode.toString()).read(pathNode, String.class)); + System.out.println(pathNode); // check, if this node exists or has an empty value. - if (JsonPath.parse(expectedNode.toString()).read(pathNode, String.class).isEmpty()) + if (JsonPath.parse(expectedNode.toString()).read(pathNode, String.class) == null || JsonPath.parse(expectedNode.toString()).read(pathNode, String.class).isEmpty()) ((ArrayNode) diff).remove(i); else if (JsonPath.parse(actualNode.toString()).read(pathNode, String.class) == null || JsonPath.parse(actualNode.toString()).read(pathNode, String.class).isEmpty()) ((ArrayNode) diff).remove(i); @@ -155,4 +98,67 @@ public class OpenAPIMain_test { } return true; } + + protected static void recursiveTest(File file) throws Exception { + if ( file.isDirectory() ) { + for ( File f : file.listFiles() ) + recursiveTest(f); + } else if ( file.isFile() && file.getPath().contains("yaml") ) { + OpenAPIObject jastAddObject; + OpenAPI POJOOpenAPI; + ObjectMapper mapper = new ObjectMapper(); + List<String> validation; + /* + String writerName = genDir + file; + writerName = writerName.substring(0, writerName.length() - 5); + FileWriter expectedWriter = new FileWriter(writerName + "-expected.json"); + FileWriter actualWriter = new FileWriter(writerName + "-actual.json"); + */ + + // parsed openAPI object with swagger-parser + SwaggerParseResult result = new OpenAPIParser().readLocation(file.getPath(), null, null); + POJOOpenAPI = result.getOpenAPI(); + System.out.println("Loading expression DSL file '" + file + "'."); + + + // validation of OpenAPI in POJO + JsonNode expectedNode = mapper.readTree(Json.mapper().writeValueAsString(POJOOpenAPI)); + validation = new OpenAPIV3Parser().readContents(expectedNode.toString()).getMessages(); + if ( validation.size() != 0 ) { + System.out.println("validation failed!"); + for ( String s : validation ) + System.out.println(s); + } + else + System.out.println("validated!"); + + // save expected object + //expectedWriter.write(expectedNode.toPrettyString()); + //expectedWriter.close(); + + // OpenAPI in POJO to OpenAPI in JastAdd + jastAddObject = OpenAPIObject.parseOpenAPI(POJOOpenAPI); + + // OpenAPI in JastAdd to OpenAPI in POJO + OpenAPI transformedAPI = OpenAPIObject.reverseOpenAPI(jastAddObject); + + // validation of transferred OpenAPI + JsonNode actualNode = mapper.readTree(Json.mapper().writeValueAsString(transformedAPI)); + validation = new OpenAPIV3Parser().readContents(actualNode.toString()).getMessages(); + if ( validation.size() != 0 ) { + System.out.println("validation failed!"); + for ( String s : validation ) + System.out.println(s); + } + else + System.out.println("validated"); + + // save generated object + //actualWriter.write(actualNode.toPrettyString()); + //actualWriter.close(); + + // compare if api (source object) is equivalent to api3 (generated object) + compareJson(expectedNode, actualNode, Paths.get(file.getPath())); + } + } } \ No newline at end of file