wso2 / api-manager

All issues, tasks, improvements and new features of WSO2 API Manager
Apache License 2.0
34 stars 8 forks source link

Mock Implementation with API Gateway not working as expected for Swagger definitions #1585

Open ashera96 opened 1 year ago

ashera96 commented 1 year ago

Description

Mock implementation feature is not working as expected when using a swagger definition as the OpenAPI URL. Refer to the reproducing steps mentioned below. By the looks of the stack trace, it seems to be a OAS2Parser related issue.

Steps to Reproduce

  1. Follow step 1 mentioned in doc [1] but by using [2] as the OpenAPI URL.
  2. Move on to step 2. Note that when we select Mock Implementation as the endpoint type and click Add, the following stack trace is logged in the server. Even though this is logged, the UI shows no errors.
com.fasterxml.jackson.core.JsonGenerationException: Can not write a string, expecting field name (context: Object)
    at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:2733)
    at com.fasterxml.jackson.core.json.JsonGeneratorImpl._reportCantWriteValueExpectName(JsonGeneratorImpl.java:262)
    at com.fasterxml.jackson.core.json.JsonGeneratorImpl._verifyPrettyValueWrite(JsonGeneratorImpl.java:252)
    at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._verifyValueWrite(WriterBasedJsonGenerator.java:942)
    at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeString(WriterBasedJsonGenerator.java:421)
    at io.swagger.inflector.processors.JsonNodeExampleSerializer.writeValue(JsonNodeExampleSerializer.java:160)
    at io.swagger.inflector.processors.JsonNodeExampleSerializer.writeTo(JsonNodeExampleSerializer.java:82)
    at io.swagger.inflector.processors.JsonNodeExampleSerializer.writeValue(JsonNodeExampleSerializer.java:93)
    at io.swagger.inflector.processors.JsonNodeExampleSerializer.writeTo(JsonNodeExampleSerializer.java:72)
    at io.swagger.inflector.processors.JsonNodeExampleSerializer.serialize(JsonNodeExampleSerializer.java:56)
    at io.swagger.inflector.processors.JsonNodeExampleSerializer.serialize(JsonNodeExampleSerializer.java:38)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1572)
    at com.fasterxml.jackson.databind.ObjectWriter._writeValueAndClose(ObjectWriter.java:1273)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1140)
    at io.swagger.util.Json.pretty(Json.java:23)
    at org.wso2.carbon.apimgt.impl.definitions.OAS2Parser.getSchemaExample_aroundBody6(OAS2Parser.java:213)
    at org.wso2.carbon.apimgt.impl.definitions.OAS2Parser.getSchemaExample(OAS2Parser.java:1)
    at org.wso2.carbon.apimgt.impl.definitions.OAS2Parser.generateExample_aroundBody4(OAS2Parser.java:177)
    at org.wso2.carbon.apimgt.impl.definitions.OAS2Parser.generateExample(OAS2Parser.java:1)
    at org.wso2.carbon.apimgt.impl.definitions.OASParserUtil.generateExamples_aroundBody4(OASParserUtil.java:231)
    at org.wso2.carbon.apimgt.impl.definitions.OASParserUtil.generateExamples(OASParserUtil.java:1)
    at org.wso2.carbon.apimgt.rest.api.publisher.v1.impl.ApisApiServiceImpl.generateMockScripts(ApisApiServiceImpl.java:3428)
    at org.wso2.carbon.apimgt.rest.api.publisher.v1.ApisApi.generateMockScripts(ApisApi.java:597)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)

[1] https://apim.docs.wso2.com/en/4.2.0/design/prototype-api/create-mocked-js-api/#step-1-create-the-interface-of-the-api [2] https://petstore.swagger.io/v2/swagger.json

Affected Component

APIM

Version

4.2.0

Environment Details (with versions)

No response

Relevant Log Output

No response

Related Issues

No response

Suggested Labels

No response

IsuruMaduranga commented 1 year ago

Hi all,

  1. The error is reproducible in APIM 4.1.0-GA, 4.1.0-U2-updated and 4.2.0-RC1
  2. Error is coming from the io.swagger.util.Json.pretty ( swagger-core-1.6.9 )
  3. What's triggering the issue is defining xml as type: string in array data type. As an example in petsstore-swagger-v2, photoUrls is defined as follows.
"photoUrls": {
    "type": "array",
    "xml": {
        "wrapped": true
    },
    "items": {
        "type": "string",
        "xml": {
            "name": "photoUrl"
        }
    }
}
  1. As a temporary workaround xml needs to be defined as type: object
"photoUrls": {
    "type": "array",
    "xml": {
        "wrapped": true
    },
    "items": {
        "type": "object",
        "xml": {
            "name": "photoUrl"
        }
    }
}
  1. Changing the swagger parser may fix the issue. I will try this out.

Thanks, Isuru