Search in sources :

Example 1 with ServerVariable

use of io.apicurio.datamodels.core.models.common.ServerVariable in project apicurio-data-models by Apicurio.

the class DataModelReader method readServer.

/**
 * Reads a server model.
 * @param json
 * @param node
 */
public void readServer(Object json, Server node) {
    String url = JsonCompat.consumePropertyString(json, Constants.PROP_URL);
    String description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
    Object variables = JsonCompat.consumeProperty(json, Constants.PROP_VARIABLES);
    node.url = url;
    node.description = description;
    if (variables != null) {
        JsonCompat.keys(variables).forEach(key -> {
            Object serverVariable = JsonCompat.consumeProperty(variables, key);
            ServerVariable serverVariableModel = node.createServerVariable(key);
            this.readServerVariable(serverVariable, serverVariableModel);
            node.addServerVariable(key, serverVariableModel);
        });
    }
    this.readExtensions(json, node);
    this.readExtraProperties(json, node);
}
Also used : ServerVariable(io.apicurio.datamodels.core.models.common.ServerVariable)

Example 2 with ServerVariable

use of io.apicurio.datamodels.core.models.common.ServerVariable in project apicurio-data-models by Apicurio.

the class IoTestRunner method cloneNode.

static Node cloneNode(Node node) {
    Constructor<? extends Node> constructor;
    Node clonedNode = null;
    try {
        if (node instanceof INamed) {
            constructor = node.getClass().getConstructor(String.class);
            clonedNode = constructor.newInstance(((INamed) node).getName());
        } else if (node instanceof OasPathItem) {
            constructor = node.getClass().getConstructor(String.class);
            clonedNode = constructor.newInstance(((OasPathItem) node).getPath());
        } else if (node instanceof Operation) {
            constructor = node.getClass().getConstructor(String.class);
            clonedNode = constructor.newInstance(((Operation) node).getType());
        } else if (node instanceof Oas20PropertySchema) {
            constructor = node.getClass().getConstructor(String.class);
            clonedNode = constructor.newInstance(((Oas20PropertySchema) node).getPropertyName());
        } else if (node instanceof Oas30PropertySchema) {
            constructor = node.getClass().getConstructor(String.class);
            clonedNode = constructor.newInstance(((Oas30PropertySchema) node).getPropertyName());
        } else if (node instanceof ServerVariable) {
            constructor = node.getClass().getConstructor(String.class);
            clonedNode = constructor.newInstance(((ServerVariable) node).getName());
        } else {
            constructor = node.getClass().getConstructor();
            clonedNode = constructor.newInstance();
        }
    } catch (SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        System.err.println("Skipping partial I/O for: " + node.getClass().getSimpleName());
    }
    if (clonedNode != null) {
        clonedNode._parent = node._parent;
        clonedNode._ownerDocument = node.ownerDocument();
        if (clonedNode instanceof Document) {
            clonedNode._ownerDocument = (Document) clonedNode;
        }
    }
    return clonedNode;
}
Also used : Node(io.apicurio.datamodels.core.models.Node) Operation(io.apicurio.datamodels.core.models.common.Operation) Document(io.apicurio.datamodels.core.models.Document) ServerVariable(io.apicurio.datamodels.core.models.common.ServerVariable) Oas20PropertySchema(io.apicurio.datamodels.openapi.v2.models.Oas20Schema.Oas20PropertySchema) InvocationTargetException(java.lang.reflect.InvocationTargetException) OasPathItem(io.apicurio.datamodels.openapi.models.OasPathItem) Oas30PropertySchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30PropertySchema) INamed(io.apicurio.datamodels.core.models.common.INamed)

Example 3 with ServerVariable

use of io.apicurio.datamodels.core.models.common.ServerVariable in project syndesis by syndesisio.

the class Oas30PropertyGeneratorsTest method shouldDetermineHostFromSpecification.

@Test
public void shouldDetermineHostFromSpecification() {
    Oas30Document openApiDoc = new Oas30Document();
    Server server = openApiDoc.addServer("{scheme}://api.example.com", "TestServer");
    ServerVariable schemes = server.createServerVariable("scheme");
    schemes.default_ = "https";
    schemes.enum_ = new ArrayList<>();
    schemes.enum_.add("https");
    server.addServerVariable("scheme", schemes);
    assertThat(GENERATOR.determineHost(new OpenApiModelInfo.Builder().model(openApiDoc).build())).isEqualTo("https://api.example.com");
    schemes.enum_.add("http");
    assertThat(GENERATOR.determineHost(new OpenApiModelInfo.Builder().model(openApiDoc).build())).isEqualTo("https://api.example.com");
    schemes.default_ = "http";
    assertThat(GENERATOR.determineHost(new OpenApiModelInfo.Builder().model(openApiDoc).build())).isEqualTo("http://api.example.com");
}
Also used : Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) Server(io.apicurio.datamodels.core.models.common.Server) OpenApiModelInfo(io.syndesis.server.api.generator.openapi.OpenApiModelInfo) ServerVariable(io.apicurio.datamodels.core.models.common.ServerVariable) Test(org.junit.Test)

Example 4 with ServerVariable

use of io.apicurio.datamodels.core.models.common.ServerVariable in project syndesis by syndesisio.

the class Oas30ModelHelperTest method getOpenApiDocWithUrl.

private static Oas30Document getOpenApiDocWithUrl(String url, ServerVariable... variables) {
    Oas30Document openApiDoc = new Oas30Document();
    Server server = openApiDoc.addServer(url, "Dummy for testing");
    for (ServerVariable variable : variables) {
        server.addServerVariable(variable._name, variable);
    }
    return openApiDoc;
}
Also used : Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) Server(io.apicurio.datamodels.core.models.common.Server) ServerVariable(io.apicurio.datamodels.core.models.common.ServerVariable) Oas30ServerVariable(io.apicurio.datamodels.openapi.v3.models.Oas30ServerVariable)

Example 5 with ServerVariable

use of io.apicurio.datamodels.core.models.common.ServerVariable in project syndesis by syndesisio.

the class Oas30PropertyGeneratorsTest method shouldDetermineHostFromSpecificationUrl.

@Test
public void shouldDetermineHostFromSpecificationUrl() {
    final URI specificationUrl = URI.create("https://api.example.com/swagger.json");
    Oas30Document openApiDoc = new Oas30Document();
    Extension extension = new Extension();
    extension.name = OasModelHelper.URL_EXTENSION;
    extension.value = specificationUrl;
    openApiDoc.addExtension(OasModelHelper.URL_EXTENSION, extension);
    assertThat(GENERATOR.determineHost(new OpenApiModelInfo.Builder().model(openApiDoc).build())).isEqualTo("https://api.example.com");
    Server server = openApiDoc.addServer("{scheme}://api.example.com", "TestServer");
    ServerVariable schemes = server.createServerVariable("scheme");
    schemes.default_ = "http";
    schemes.enum_ = new ArrayList<>();
    schemes.enum_.add("http");
    server.addServerVariable("scheme", schemes);
    assertThat(GENERATOR.determineHost(new OpenApiModelInfo.Builder().model(openApiDoc).build())).isEqualTo("http://api.example.com");
    server.url = "{scheme}://api2.example.com";
    assertThat(GENERATOR.determineHost(new OpenApiModelInfo.Builder().model(openApiDoc).build())).isEqualTo("http://api2.example.com");
}
Also used : Extension(io.apicurio.datamodels.core.models.Extension) Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) Server(io.apicurio.datamodels.core.models.common.Server) OpenApiModelInfo(io.syndesis.server.api.generator.openapi.OpenApiModelInfo) URI(java.net.URI) ServerVariable(io.apicurio.datamodels.core.models.common.ServerVariable) Test(org.junit.Test)

Aggregations

ServerVariable (io.apicurio.datamodels.core.models.common.ServerVariable)6 Server (io.apicurio.datamodels.core.models.common.Server)4 Oas30Document (io.apicurio.datamodels.openapi.v3.models.Oas30Document)4 OpenApiModelInfo (io.syndesis.server.api.generator.openapi.OpenApiModelInfo)3 Test (org.junit.Test)3 Document (io.apicurio.datamodels.core.models.Document)1 Extension (io.apicurio.datamodels.core.models.Extension)1 Node (io.apicurio.datamodels.core.models.Node)1 INamed (io.apicurio.datamodels.core.models.common.INamed)1 Operation (io.apicurio.datamodels.core.models.common.Operation)1 OasPathItem (io.apicurio.datamodels.openapi.models.OasPathItem)1 Oas20PropertySchema (io.apicurio.datamodels.openapi.v2.models.Oas20Schema.Oas20PropertySchema)1 Oas30PropertySchema (io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30PropertySchema)1 Oas30ServerVariable (io.apicurio.datamodels.openapi.v3.models.Oas30ServerVariable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1