Skip to content

Commit

Permalink
Add test for Jackson XML wrapper correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed May 2, 2024
1 parent e588bdb commit df55a9d
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -4518,4 +4519,43 @@ public void testLombokAnnotations() throws IOException {
.assertMethod("equals")
;
}

@Test
public void shouldGenerateCorrectJacksonWrappedAndUnwrappedXmlAnnotations_issue2417() throws IOException {
// Arrange
final String TEST_SPEC = "src/test/resources/3_0/spring/xml-annotations-test_issue-2417.yaml";
final Path output = Files.createTempDirectory("test-2417");
output.toFile().deleteOnExit();

SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.setWithXml(true);
codegen.setOutputDir(output.toString());

DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

// Act
Map<String, File> files = generator.opts(
new ClientOptInput()
.openAPI(new OpenAPIParser().readLocation(TEST_SPEC, null, null).getOpenAPI())
.config(codegen)
)
.generate()
.stream().collect(Collectors.toMap(File::getName, Function.identity()));

// Assert
JavaFileAssert.assertThat(files.get("Pet.java"))
.assertMethod("getPhotoUrls").assertMethodAnnotations()
.containsWithNameAndAttributes("JacksonXmlProperty", Map.of("localName", "\"photoUrls\""))
.containsWithNameAndAttributes("JacksonXmlElementWrapper", Map.of("localName", "\"photoUrl\""))
.toMethod().toFileAssert()
.assertMethod("getCategories").assertMethodAnnotations()
.containsWithNameAndAttributes("JacksonXmlProperty", Map.of("localName", "\"Category\""))
.containsWithNameAndAttributes("JacksonXmlElementWrapper", Map.of("useWrapping", "false"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
openapi: 3.0.0
info:
description: >-
This spec is mainly for testing JAXB & Jackson XML annotation being generated correctly. \n
Some guidance: \n
Jackson annotations: https://www.baeldung.com/jackson-xml-serialization-and-deserialization \n
JAXB annotations: https://howtodoinjava.com/jaxb/xmlelementwrapper-annotation/
\
version: 1.0.0
title: OpenAPI Petstore
paths:
/foo:
get:
operationId: foo
responses:
'200':
description: response
content:
application/xml:
schema:
type: object
properties:
string:
$ref: '#/components/schemas/Pet'
components:
requestBodies:
Pet:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
required: true
schemas:
Category:
type: object
required:
- name
properties:
id:
type: integer
format: int64
name:
type: string
default: default-name
xml:
name: Category
Tag:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: Tag
Pet:
type: object
required:
- name
- photoUrls
properties:
id:
type: integer
format: int64
x-is-unique: true
categories:
type: array
items:
$ref: '#/components/schemas/Category'
xml:
wrapped: false # This is the default value as per OAI spec anyway, just being explicit here for the test
name:
type: string
example: doggie
photoUrls:
type: array
xml:
name: photoUrl
wrapped: true
items:
type: string
uniqueItems: true
tags:
type: array
xml:
name: tag
wrapped: true
items:
$ref: '#/components/schemas/Tag'
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
xml:
name: Pet

0 comments on commit df55a9d

Please sign in to comment.