Skip to content

Commit

Permalink
feat(core)!: change setting use-fqn default to true (#592)
Browse files Browse the repository at this point in the history
* feat(core)!: change setting use-fqn default to true

* test(core): update after refactor

* feat(core): handle useFqn=true in xml generator

* chore(core): formatting
  • Loading branch information
timonback authored Feb 23, 2024
1 parent 4953771 commit 1a62388
Show file tree
Hide file tree
Showing 37 changed files with 264 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public enum InitMode {
* useFqn = true -> java.lang.String
* useFqn = false -> String
*/
private boolean useFqn = false;
private boolean useFqn = true;

@Nullable
private Endpoint endpoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,11 @@ private String registerString() {

private <R> R runWithFqnSetting(Function<Void, R> callable) {
boolean previousUseFqn = TypeNameResolver.std.getUseFqn();
if (properties.isUseFqn()) {
TypeNameResolver.std.setUseFqn(true);
}
TypeNameResolver.std.setUseFqn(properties.isUseFqn());

R result = callable.apply(null);

if (properties.isUseFqn()) {
TypeNameResolver.std.setUseFqn(previousUseFqn);
}
TypeNameResolver.std.setUseFqn(previousUseFqn);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ public boolean canHandle(String contentType) {

@Override
public R fromSchema(Schema schema, Map<String, Schema> definitions) {
try {
exampleValueGenerator.initialize();
exampleValueGenerator.initialize();

T generatedExample = buildExample(schema.getName(), schema, definitions, new HashSet<>());
String schemaName = exampleValueGenerator.lookupSchemaName(schema);
try {
T generatedExample = buildExample(schemaName, schema, definitions, new HashSet<>());

return exampleValueGenerator.prepareForSerialization(schema.getName(), generatedExample);
return exampleValueGenerator.prepareForSerialization(schemaName, generatedExample);
} catch (ExampleGeneratingException ex) {
log.info("Failed to build example for schema {}", schema.getName(), ex);
log.info("Failed to build example for schema {}", schemaName, ex);
}
return null;
}
Expand Down Expand Up @@ -72,7 +73,8 @@ private T getExampleValueFromSchemaAnnotation(Schema schema) {
}

// Return directly, when we have processed this before
T processedExample = exampleValueGenerator.getExampleOrNull(schema.getName(), exampleValue);
T processedExample =
exampleValueGenerator.getExampleOrNull(exampleValueGenerator.lookupSchemaName(schema), exampleValue);
if (processedExample != null) {
return processedExample;
}
Expand Down Expand Up @@ -116,7 +118,8 @@ private T getExampleValueFromSchemaAnnotation(Schema schema) {
}

private T buildArrayExample(Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
T arrayItem = buildExample(schema.getName(), schema.getItems(), definitions, visited);
String name = exampleValueGenerator.lookupSchemaName(schema);
T arrayItem = buildExample(name, schema.getItems(), definitions, visited);

return exampleValueGenerator.createArrayExample(arrayItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -35,6 +36,11 @@ public void initialize() {
// Nothing to do
}

@Override
public String lookupSchemaName(Schema schema) {
return schema.getName();
}

@NotNull
@Override
public JsonNode createBooleanExample() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.stavshamir.springwolf.schemas.example;

import io.swagger.v3.oas.models.media.Schema;

import java.util.List;

/**
Expand Down Expand Up @@ -34,6 +36,8 @@ interface ExampleValueGenerator<T, R> {
*/
void initialize();

String lookupSchemaName(Schema schema);

/**
* @return The serializable representation of the example (object for json & yaml, string for others)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.stavshamir.springwolf.schemas.example;

import io.swagger.v3.oas.models.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -50,6 +51,14 @@ public void initialize() {
}
}

@Override
public String lookupSchemaName(Schema schema) {
if (schema.getXml() != null) {
return schema.getXml().getName();
}
return schema.getName();
}

@Override
public Node createIntegerExample(Integer value) {
return document.createTextNode(value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.models.media.Schema;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -26,6 +27,11 @@ public boolean canHandle(String contentType) {
@Override
public void initialize() {}

@Override
public String lookupSchemaName(Schema schema) {
return exampleJsonValueGenerator.lookupSchemaName(schema);
}

@Override
public String prepareForSerialization(String name, JsonNode exampleObject) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,47 @@ void polymorphicDiscriminatorFieldIsHandled() {
.containsOnlyKeys(
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Payload");
Map<String, SchemaObject> schemas = asyncAPI.getComponents().getSchemas();
assertThat(schemas).containsOnlyKeys("HeadersNotDocumented", "Payload", "Pet", "Cat", "Dog");
assertThat(schemas)
.containsOnlyKeys(
"HeadersNotDocumented",
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Payload",
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Pet",
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Cat",
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Dog");

assertThat(schemas.get("Pet").getDiscriminator()).isEqualTo("type");
assertThat(schemas.get("Cat").getAllOf().get(0).getReference().getRef())
.isEqualTo("#/components/schemas/Pet");
assertThat(schemas.get("Cat").getAllOf().get(1).getSchema().getProperties())
assertThat(schemas.get(
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Pet")
.getDiscriminator())
.isEqualTo("type");
assertThat(schemas.get(
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Cat")
.getAllOf()
.get(0)
.getReference()
.getRef())
.isEqualTo(
"#/components/schemas/io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Pet");
assertThat(schemas.get(
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Cat")
.getAllOf()
.get(1)
.getSchema()
.getProperties())
.containsOnlyKeys("catSpecificField");
assertThat(schemas.get("Dog").getAllOf().get(0).getReference().getRef())
.isEqualTo("#/components/schemas/Pet");
assertThat(schemas.get("Dog").getAllOf().get(1).getSchema().getProperties())
assertThat(schemas.get(
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Dog")
.getAllOf()
.get(0)
.getReference()
.getRef())
.isEqualTo(
"#/components/schemas/io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Pet");
assertThat(schemas.get(
"io.github.stavshamir.springwolf.integrationtests.application.polymorphic.PolymorphicPayloadApplication$Dog")
.getAllOf()
.get(1)
.getSchema()
.getProperties())
.containsOnlyKeys("dogSpecificField");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ void classWithSchemaAnnotation() {
}

@Test
void getDefinitionWithFqnClassName() throws IOException {
void getDefinitionWithoutFqnClassName() throws IOException {
// given
SpringwolfConfigProperties properties = new SpringwolfConfigProperties();
properties.setUseFqn(true);
properties.setUseFqn(false);

ComponentsService componentsServiceWithFqn =
new DefaultComponentsService(List.of(), List.of(), new SwaggerSchemaUtil(), properties);
Expand All @@ -106,9 +106,8 @@ void getDefinitionWithFqnClassName() throws IOException {

// then
System.out.println("Got: " + actualDefinitions);
String fqnClassName = clazz.getName();
assertThat(actualDefinitions).contains(fqnClassName);
assertThat(fqnClassName.length()).isGreaterThan(clazz.getSimpleName().length());
assertThat(actualDefinitions).contains(clazz.getSimpleName());
assertThat(actualDefinitions).doesNotContain(clazz.getCanonicalName());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private String jsonResource(String path) throws IOException {

@Data
@NoArgsConstructor
@XmlRootElement(name = "SimpleFoo")
private static class SimpleFoo {
private String s;
private boolean b;
Expand All @@ -120,7 +121,7 @@ private static class SimpleFoo {
@Data
@NoArgsConstructor
@Schema(description = "foo model")
@XmlRootElement(name = "documentedSimpleFoo")
@XmlRootElement(name = "DocumentedSimpleFoo")
private static class DocumentedSimpleFoo {
@Schema(description = "s field", example = "s value", requiredMode = Schema.RequiredMode.REQUIRED)
private String s;
Expand All @@ -146,23 +147,26 @@ private static class DocumentedSimpleFoo {

@Data
@NoArgsConstructor
@XmlRootElement
@XmlRootElement(name = "CompositeFoo")
private static class CompositeFoo {
private String s;
private SimpleFoo f;
}

@Data
@NoArgsConstructor
@XmlRootElement(name = "ArrayFoo")
private static class ArrayFoo {
private List<SimpleFoo> fList;
}

@NoArgsConstructor
@XmlRootElement(name = "ListWrapper")
private static class ListWrapper extends ArrayList<String> {}

@Data
@NoArgsConstructor
@XmlRootElement(name = "FooWithEnum")
private static class FooWithEnum {
private String s;
private Bar b;
Expand All @@ -175,6 +179,7 @@ private enum Bar {

@Data
@NoArgsConstructor
@XmlRootElement(name = "ComplexFoo")
private static class ComplexFoo {
private String s;
private Boolean b;
Expand All @@ -186,6 +191,7 @@ private static class ComplexFoo {

@Data
@NoArgsConstructor
@XmlRootElement(name = "Nested")
private static class Nested {
private String ns;
private List<Integer> nli;
Expand All @@ -197,6 +203,7 @@ private static class Nested {

@Data
@NoArgsConstructor
@XmlRootElement(name = "Cyclic")
private static class Cyclic {

@Nullable
Expand All @@ -205,13 +212,15 @@ private static class Cyclic {

@Data
@NoArgsConstructor
@XmlRootElement(name = "MyClass")
private static class MyClass {
private String s;
}
}
}

@Nested
@XmlRootElement(name = "SchemaWithOneOf")
class SchemaWithOneOf {
@Test
void testSchemaWithOneOf() throws IOException {
Expand All @@ -226,6 +235,7 @@ void testSchemaWithOneOf() throws IOException {

@Data
@NoArgsConstructor
@XmlRootElement(name = "SchemaAnnotationFoo")
public class SchemaAnnotationFoo {
private String field;
private AnyOf anyOf;
Expand All @@ -234,23 +244,28 @@ public class SchemaAnnotationFoo {
}

@Schema(anyOf = {ImplementationOne.class, ImplementationTwo.class})
@XmlRootElement(name = "AnyOf")
public interface AnyOf {}

@Schema(allOf = {ImplementationOne.class, ImplementationTwo.class})
@XmlRootElement(name = "AllOf")
public interface AllOf {}

@Schema(oneOf = {ImplementationOne.class, ImplementationTwo.class})
@XmlRootElement(name = "OneOf")
public interface OneOf {}

@Data
@NoArgsConstructor
@XmlRootElement(name = "ImplementationOne")
public class ImplementationOne {
private String firstOne;
private String secondOne;
}

@Data
@NoArgsConstructor
@XmlRootElement(name = "ImplementationTwo")
public class ImplementationTwo {
private Integer firstTwo;
private Boolean secondTwo;
Expand Down Expand Up @@ -294,6 +309,7 @@ public class StringEnvelop {

@Data
@NoArgsConstructor
@XmlRootElement(name = "EnvelopWithMultipleAsyncApiPayloadAnnotations")
public class EnvelopWithMultipleAsyncApiPayloadAnnotations {
@AsyncApiPayload
Integer otherField;
Expand Down
Loading

0 comments on commit 1a62388

Please sign in to comment.