Skip to content

Commit

Permalink
flex other parts of the API
Browse files Browse the repository at this point in the history
Signed-off-by: Bartosz Michalik <bartosz.michalik@amartus.com>

Signed-off-by: Bartosz Michalik <bartosz.michalik@gmail.com>
  • Loading branch information
bartoszm committed Mar 13, 2023
1 parent c83a2e3 commit a395837
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.amartus.sonata.blender.impl.ProductSpecReader;
import com.amartus.sonata.blender.impl.util.Pair;
import com.amartus.sonata.blender.impl.util.PathResolver;
import com.amartus.sonata.blender.parser.DeserializerProvider;
import com.github.rvesse.airline.annotations.Option;
import com.github.rvesse.airline.annotations.restrictions.Once;
import com.github.rvesse.airline.annotations.restrictions.RequireOnlyOne;
Expand Down Expand Up @@ -93,7 +94,7 @@ public abstract class AbstractBlend {

protected Map<String, Schema> toProductSpecifications() {
return toSchemaPaths(blendingSchemas())
.flatMap(schema -> new ProductSpecReader(modelToAugment, schema.first(), schema.second()).readSchemas().entrySet().stream())
.flatMap(schema -> new ProductSpecReader(modelToAugment, schema.first(), schema.second(), new DeserializerProvider(), ProductSpecReader.defaultOptions()).readSchemas().entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> {
if (a.equals(b)) return a;
throw new IllegalArgumentException(String.format("Object for the same key does not match %s %s", a, b));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/amartus/sonata/blender/cmd/Merge.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.amartus.sonata.blender.impl.util.IdSchemaResolver;
import com.amartus.sonata.blender.impl.util.PathResolver;
import com.amartus.sonata.blender.impl.util.SerializationUtils;
import com.amartus.sonata.blender.parser.DeserializerProvider;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option;
import com.github.rvesse.airline.annotations.restrictions.Once;
Expand Down Expand Up @@ -162,7 +163,7 @@ protected Map<String, Schema> toProductSpecifications() {
var paths = resolver.toSchemaPaths(blendedSchema.stream());

return paths
.flatMap(schema -> new ProductSpecReader(modelToAugment, schema.first(), schema.second()).readSchemas().entrySet().stream())
.flatMap(schema -> new ProductSpecReader(modelToAugment, schema.first(), schema.second(), new DeserializerProvider(), ProductSpecReader.defaultOptions()).readSchemas().entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> {
if (a.equals(b)) return a;
throw new IllegalArgumentException(String.format("Object for the same key does not match %s %s", a, b));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,23 @@ public class ProductSpecReader {
private final List<ProductSpecificationNamingStrategy> namingStrategies;
private final Charset charset = StandardCharsets.UTF_8;
private final String fragment;
private final DeserializerProvider deserializerProvider;

private final ParseOptions options;

public ProductSpecReader(String modelToAugment, Path schemaLocation) {
this(modelToAugment, schemaLocation, "");
this(modelToAugment, schemaLocation, "", new DeserializerProvider(), defaultOptions());
}

public ProductSpecReader(String modelToAugment, Path schemaLocation, DeserializerProvider deserializerProvider) {
this(modelToAugment, schemaLocation, "", deserializerProvider, defaultOptions());
}

public ProductSpecReader(String modelToAugment, Path schemaLocation, String fragment) {
public ProductSpecReader(String modelToAugment, Path schemaLocation, String fragment, DeserializerProvider deserializerProvider, ParseOptions options) {
this.schemaPath = Objects.requireNonNull(schemaLocation).toAbsolutePath();
this.fragment = Objects.requireNonNull(fragment);
this.deserializerProvider = Objects.requireNonNull(deserializerProvider);
this.options = Objects.requireNonNull(options);
if (!Files.exists(this.schemaPath)) {
throw new IllegalArgumentException("Path " + this.schemaPath + " does not exists");
}
Expand All @@ -77,6 +86,13 @@ public ProductSpecReader(String modelToAugment, Path schemaLocation, String frag
).collect(Collectors.toList());
}

public static ParseOptions defaultOptions() {
var options = new ParseOptions();
options.setResolve(true);
options.setValidateExternalRefs(true);
return options;
}

public Map<String, Schema<?>> readSchemas() {
log.info("Resolving {}", this.schemaPath);
OpenAPI api = new OpenAPI();
Expand Down Expand Up @@ -172,11 +188,8 @@ private String toRefName(Schema schema) {

protected Map<String, Schema<?>> resolve(Schema schema) {
var parentFile = schemaPath.toString();
var options = new ParseOptions();
options.setResolve(true);
options.setValidateExternalRefs(true);
var oas = new OpenAPI().schema(KEY, schema);
var cache = new ResolverCache(oas, parentFile, options, new DeserializerProvider());
var cache = new ResolverCache(oas, parentFile, options, deserializerProvider);

OpenAPIResolver r = new OpenAPIResolver(new OpenAPI().schema(KEY, schema), cache, null);
var res = new SwaggerParseResult().messages(new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Schema getSchema(JsonNode node, String location, ParseResult result) {
return schema;
}
}
OpenAPIDeserializer deserializer() {
public OpenAPIDeserializer deserializer() {
return new AmartusDeserializer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.amartus.sonata.blender.impl;

import com.amartus.Utils;
import com.amartus.sonata.blender.parser.DeserializerProvider;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.junit.jupiter.api.Test;
Expand All @@ -35,7 +36,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

class ProductSpecReaderTest {

private final DeserializerProvider deserializerProvider = new DeserializerProvider();
@Test
public void testReaderForSchema() {
var dirPath = Utils.toPath("mini-model").toAbsolutePath();
Expand All @@ -58,7 +59,7 @@ public void testReaderForSchemaUsingRelativePaths() {
@Test
public void testReaderForSchemaOas() {
var dirPath = Utils.toPath("mini-model");
var schemas = new ProductSpecReader("testToAugment", dirPath.resolve("model-oas.yaml"), "#/components/schemas/ModelOAS")
var schemas = new ProductSpecReader("testToAugment", dirPath.resolve("model-oas.yaml"), "#/components/schemas/ModelOAS", deserializerProvider, ProductSpecReader.defaultOptions())
.readSchemas();
singleRootSchema(schemas);
assertEquals(7, schemas.size());
Expand All @@ -67,7 +68,7 @@ public void testReaderForSchemaOas() {
@Test
public void testReadComposedModel() {
var dirPath = Utils.toPath("ref-model");
var schemas = new ProductSpecReader("testToAugment", dirPath.resolve("root.yaml"), "#/components/schemas/Root")
var schemas = new ProductSpecReader("testToAugment", dirPath.resolve("root.yaml"), "#/components/schemas/Root", deserializerProvider, ProductSpecReader.defaultOptions())
.readSchemas();
singleRootSchema(schemas);
assertEquals(6, schemas.size());
Expand Down

0 comments on commit a395837

Please sign in to comment.