Search in sources :

Example 26 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processRefToExternalExample.

public String processRefToExternalExample(String $ref, RefFormat refFormat) {
    String renamedRef = cache.getRenamedRef($ref);
    if (renamedRef != null) {
        return renamedRef;
    }
    final Example example = cache.loadRef($ref, refFormat, Example.class);
    if (example == null) {
        // stop!  There's a problem.  retain the original ref
        LOGGER.warn("unable to load model reference from `" + $ref + "`.  It may not be available " + "or the reference isn't a valid model schema");
        return $ref;
    }
    String newRef;
    if (openAPI.getComponents() == null) {
        openAPI.setComponents(new Components());
    }
    Map<String, Example> examples = openAPI.getComponents().getExamples();
    if (examples == null) {
        examples = new LinkedHashMap<>();
    }
    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
    Example existingExample = examples.get(possiblyConflictingDefinitionName);
    if (existingExample != null) {
        LOGGER.debug("A model for " + existingExample + " already exists");
        if (existingExample.get$ref() != null) {
            // use the new model
            existingExample = null;
        }
    }
    newRef = possiblyConflictingDefinitionName;
    cache.putRenamedRef($ref, newRef);
    if (existingExample == null) {
        // don't overwrite existing model reference
        openAPI.getComponents().addExamples(newRef, example);
        cache.addReferencedKey(newRef);
        String file = $ref.split("#/")[0];
        if (example.get$ref() != null) {
            RefFormat format = computeRefFormat(example.get$ref());
            if (isAnExternalRefFormat(format)) {
                example.set$ref(processRefToExternalExample(example.get$ref(), format));
            } else {
                processRefToExternalExample(file + example.get$ref(), RefFormat.RELATIVE);
            }
        }
    }
    return newRef;
}
Also used : Components(io.swagger.v3.oas.models.Components) Example(io.swagger.v3.oas.models.examples.Example) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

Example 27 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processRefSchema.

private void processRefSchema(Schema subRef, String externalFile) {
    RefFormat format = computeRefFormat(subRef.get$ref());
    if (!isAnExternalRefFormat(format)) {
        subRef.set$ref(RefType.SCHEMAS.getInternalPrefix() + processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
        return;
    }
    String $ref = subRef.get$ref();
    String subRefExternalPath = getExternalPath(subRef.get$ref()).orElse(null);
    if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
        $ref = constructRef(subRef, externalFile);
        subRef.set$ref($ref);
    } else {
        processRefToExternalSchema($ref, format);
    }
}
Also used : RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

Example 28 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class ExternalRefProcessorTest method testNestedExternalRefs.

@Test
public void testNestedExternalRefs(@Injectable final Schema mockedModel) {
    final RefFormat refFormat = RefFormat.URL;
    // Swagger test instance
    OpenAPI testedOpenAPI = new OpenAPI();
    // Start with customer, add address property to it
    final String customerURL = "http://my.company.com/path/to/customer.json#/definitions/Customer";
    final Schema customerModel = new Schema();
    Map<String, Schema> custProps = new HashMap<>();
    Schema address = new Schema();
    final String addressURL = "http://my.company.com/path/to/address.json#/definitions/Address";
    address.set$ref(addressURL);
    custProps.put("Address", address);
    // Create a 'local' reference to something in #/definitions, this should be ignored a no longer result in a null pointer exception
    final String loyaltyURL = "#/definitions/LoyaltyScheme";
    Schema loyaltyProp = new Schema();
    loyaltyProp.set$ref(loyaltyURL);
    loyaltyProp.setName("LoyaltyCardNumber");
    List<String> required = new ArrayList<>();
    required.add("LoyaltyCardNumber");
    loyaltyProp.setRequired(required);
    custProps.put("Loyalty", loyaltyProp);
    customerModel.setProperties(custProps);
    // create address model, add Contact Ref Property to it
    final Schema addressModel = new Schema();
    Map<String, Schema> addressProps = new HashMap<>();
    Schema contact = new Schema();
    final String contactURL = "http://my.company.com/path/to/Contact.json#/definitions/Contact";
    contact.set$ref(contactURL);
    addressProps.put("Contact", contact);
    addressModel.setProperties(addressProps);
    // Create contact model, with basic type property
    final Schema contactModel = new Schema();
    Schema contactProp = new StringSchema();
    contactProp.setName("PhoneNumber");
    List<String> requiredList = new ArrayList<>();
    requiredList.add("PhoneNumber");
    contactProp.setRequired(requiredList);
    Map<String, Schema> contactProps = new HashMap<>();
    contactProps.put("PhoneNumber", contactProp);
    contactModel.setProperties(contactProps);
    new Expectations() {

        {
            cache.loadRef(customerURL, refFormat, Schema.class);
            result = customerModel;
            times = 1;
            cache.loadRef(addressURL, refFormat, Schema.class);
            result = addressModel;
            times = 1;
            cache.loadRef(contactURL, refFormat, Schema.class);
            result = contactModel;
            times = 1;
        }
    };
    new ExternalRefProcessor(cache, testedOpenAPI).processRefToExternalSchema(customerURL, refFormat);
    assertThat(testedOpenAPI.getComponents().getSchemas().get("Customer"), notNullValue());
    assertThat(testedOpenAPI.getComponents().getSchemas().get("Contact"), notNullValue());
    assertThat(testedOpenAPI.getComponents().getSchemas().get("Address"), notNullValue());
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) HashMap(java.util.HashMap) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) RefFormat(io.swagger.v3.parser.models.RefFormat) ArrayList(java.util.ArrayList) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 29 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class ExternalRefProcessorTest method testRelativeRefIncludingUrlRef.

@Test
public void testRelativeRefIncludingUrlRef(@Injectable final Schema mockedModel) throws Exception {
    final RefFormat refFormat = RefFormat.RELATIVE;
    final String url = "https://my.example.remote.url.com/globals.yaml";
    final String expectedResult = "components:\n" + "  schemas:\n" + "    link-object:\n" + "      type: object\n" + "      additionalProperties:\n" + "        \"$ref\": \"#/components/schemas/rel-data\"\n" + "    rel-data:\n" + "      type: object\n" + "      required:\n" + "      - href\n" + "      properties:\n" + "        href:\n" + "          type: string\n" + "        note:\n" + "          type: string\n" + "    result:\n" + "      type: object\n" + "      properties:\n" + "        name:\n" + "          type: string\n" + "        _links:\n" + "          \"$ref\": \"#/components/schemas/link-object\"\n" + "";
    List<AuthorizationValue> auths = null;
    new Expectations() {

        {
            RemoteUrl.urlToString(url, auths);
            times = 1;
            result = expectedResult;
        }
    };
    OpenAPI mockedOpenAPI = new OpenAPI();
    mockedOpenAPI.setComponents(new Components());
    mockedOpenAPI.getComponents().setSchemas(new HashMap<>());
    ResolverCache mockedResolverCache = new ResolverCache(mockedOpenAPI, null, null);
    ExternalRefProcessor processor = new ExternalRefProcessor(mockedResolverCache, mockedOpenAPI);
    processor.processRefToExternalSchema("./relative-with-url/relative-with-url.yaml#/relative-with-url", refFormat);
    assertThat(((Schema) mockedOpenAPI.getComponents().getSchemas().get("relative-with-url").getProperties().get("Foo")).get$ref(), is("https://my.example.remote.url.com/globals.yaml#/components/schemas/link-object"));
    assertThat(mockedOpenAPI.getComponents().getSchemas().keySet().contains("link-object"), is(true));
    assertThat(mockedOpenAPI.getComponents().getSchemas().keySet().contains("rel-data"), is(true));
    // assert that ref is relative ref is resolved. and the file path is from root yaml file.
    assertThat(((Schema) mockedOpenAPI.getComponents().getSchemas().get("relative-with-url").getProperties().get("Bar")).get$ref(), is("./relative-with-url/relative-with-local.yaml#/relative-same-file"));
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) Components(io.swagger.v3.oas.models.Components) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) RefFormat(io.swagger.v3.parser.models.RefFormat) ResolverCache(io.swagger.v3.parser.ResolverCache) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 30 with RefFormat

use of io.swagger.v3.parser.models.RefFormat in project swagger-parser by swagger-api.

the class ResolverFully method resolveParameter.

public Parameter resolveParameter(Parameter parameter) {
    String $ref = parameter.get$ref();
    RefFormat refFormat = computeRefFormat($ref);
    if (!isAnExternalRefFormat(refFormat)) {
        if (parameters != null && !parameters.isEmpty()) {
            String referenceKey = computeDefinitionName($ref);
            return parameters.getOrDefault(referenceKey, parameter);
        }
    }
    return parameter;
}
Also used : RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

Aggregations

RefFormat (io.swagger.v3.parser.models.RefFormat)38 RefUtils.computeRefFormat (io.swagger.v3.parser.util.RefUtils.computeRefFormat)30 RefUtils.isAnExternalRefFormat (io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat)29 Components (io.swagger.v3.oas.models.Components)12 Schema (io.swagger.v3.oas.models.media.Schema)8 Test (org.testng.annotations.Test)7 ResolverCache (io.swagger.v3.parser.ResolverCache)6 Expectations (mockit.Expectations)6 Example (io.swagger.v3.oas.models.examples.Example)5 PathItem (io.swagger.v3.oas.models.PathItem)4 MediaType (io.swagger.v3.oas.models.media.MediaType)4 Parameter (io.swagger.v3.oas.models.parameters.Parameter)4 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)4 OpenAPI (io.swagger.v3.oas.models.OpenAPI)3 Callback (io.swagger.v3.oas.models.callbacks.Callback)3 Header (io.swagger.v3.oas.models.headers.Header)3 Link (io.swagger.v3.oas.models.links.Link)3 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)3 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)3 Map (java.util.Map)3