Skip to content

Commit

Permalink
feat(openapi) OpenAPI Schema extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Apr 6, 2024
1 parent 46b5e87 commit 5e7e58b
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.asyncapi.v3.jackson.schema.openapi.OpenAPISchemaAdditionalPropertiesDeserializer;
import com.asyncapi.v3.jackson.schema.openapi.OpenAPISchemaAnyValueDeserializer;
import com.asyncapi.v3.schema.openapi.properties.Discriminator;
import com.asyncapi.v3.schema.openapi.properties.Extensions;
import com.asyncapi.v3.schema.openapi.properties.ExternalDocumentation;
import com.asyncapi.v3.schema.openapi.properties.XML;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import org.jetbrains.annotations.Nullable;

import java.math.BigDecimal;
Expand All @@ -34,7 +35,8 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OpenAPISchema {
@EqualsAndHashCode(callSuper = true)
public class OpenAPISchema extends Extensions {

/**
* Schema name.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.asyncapi.v3.schema.openapi;
package com.asyncapi.v3.schema.openapi.properties;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.asyncapi.v3.schema.openapi.properties;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
* @see <a href="https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#specification-extensions">Specification Extensions</a>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({"extensions"})
public class Extensions {

private static final Pattern extensionPropertyNamePattern = Pattern.compile("^x-.*");

/**
* Extension fields in the form x-extension-field-name for the exposed API.
*/
@Nullable
@JsonAnyGetter
protected Map<String, Object> extensions;

@JsonAnySetter
protected final void readExtensionProperty(String name, Object value) {
if (extensionPropertyNamePattern.matcher(name).matches()) {
if (extensions == null) {
extensions = new HashMap<>();
}

extensions.put(name, value);
} else {
throw new IllegalArgumentException(String.format("\"%s\" is not valid extension property", name));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.asyncapi.v3.schema.openapi;
package com.asyncapi.v3.schema.openapi.properties;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -22,7 +19,8 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExternalDocumentation {
@EqualsAndHashCode(callSuper = true)
public class ExternalDocumentation extends Extensions {

/**
* A short description of the target documentation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.asyncapi.v3.schema.openapi;
package com.asyncapi.v3.schema.openapi.properties;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -21,7 +18,8 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class XML {
@EqualsAndHashCode(callSuper = true)
public class XML extends Extensions {

/**
* Replaces the name of the element/attribute used for the described schema property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.asyncapi.v3.schema.openapi

import com.asyncapi.v3.ClasspathUtils
import com.asyncapi.v3.schema.SchemaProvider
import com.asyncapi.v3.schema.openapi.properties.ExampleEnumDefaultArrayTest
import com.asyncapi.v3.schema.openapi.properties.ExampleEnumDefaultNullTest
import com.asyncapi.v3.schema.openapi.properties.*
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.Assertions
Expand Down Expand Up @@ -79,10 +78,13 @@ class OpenAPISchemaTest {
class XMLs: ArgumentsProvider {

override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> {
val extendedXML = XML(
"animal", "http://example.com/schema/sample", "sample", true, true
)
extendedXML.extensions = mapOf(Pair("x-extension-property", "value"))

return Stream.of(
Arguments.of("/json/v3/schema/openapi/xml.json", XML(
"animal", "http://example.com/schema/sample", "sample", true, true
)),
Arguments.of("/json/v3/schema/openapi/xml.json", extendedXML),
Arguments.of("/json/v3/schema/openapi/xml-attribute.json", XML.builder().attribute(true).build()),
Arguments.of("/json/v3/schema/openapi/xml-name-replacement.json", XML.builder().name("animal").build()),
Arguments.of("/json/v3/schema/openapi/xml-prefix-and-namespace.json", XML.builder()
Expand Down Expand Up @@ -115,10 +117,11 @@ class OpenAPISchemaTest {
class ExternalDocumentations: ArgumentsProvider {

override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> {
val extendedExternalDocumentation = ExternalDocumentation("Find more info here", "https://example.com")
extendedExternalDocumentation.extensions = mapOf(Pair("x-extension-property", "value"))

return Stream.of(
Arguments.of("/json/v3/schema/openapi/externaldocumentation.json", ExternalDocumentation(
"Find more info here", "https://example.com"
)),
Arguments.of("/json/v3/schema/openapi/externaldocumentation.json", extendedExternalDocumentation),
Arguments.of("/json/v3/schema/openapi/externaldocumentation-url.json", ExternalDocumentation(
null, "https://example.com"
)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.asyncapi.v3.schema.openapi

import com.asyncapi.v3.schema.SchemaProvider
import com.asyncapi.v3.schema.openapi.properties.Discriminator
import com.asyncapi.v3.schema.openapi.properties.ExternalDocumentation
import com.asyncapi.v3.schema.openapi.properties.XML
import java.math.BigDecimal

class SchemaTest: SchemaProvider {

override fun openAPISchema(): OpenAPISchema {
return OpenAPISchema.builder()
val schema = OpenAPISchema.builder()
.name("schema name")
.title("schema title")
.multipleOf(BigDecimal(2.5))
Expand Down Expand Up @@ -163,6 +166,9 @@ class SchemaTest: SchemaProvider {
)
))
.build()
schema.extensions = mapOf(Pair("x-extension-property", "value"))

return schema
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"description": "Find more info here",
"url": "https://example.com"
"url": "https://example.com",
"x-extension-property": "value"
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,6 @@
"dog": "#/components/schemas/Dog",
"monster": "https://gigantic-server.com/schemas/Monster/schema.json"
}
}
},
"x-extension-property": "value"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"namespace": "http://example.com/schema/sample",
"prefix": "sample",
"attribute": true,
"wrapped": true
"wrapped": true,
"x-extension-property": "value"
}

0 comments on commit 5e7e58b

Please sign in to comment.