Skip to content

Unable to build Go SDK from OpenAPI (failures in accounting schema) #563

@17twenty

Description

@17twenty

Uh Oh - no matter the tools, we're unable to generate a buildable SDK for Golang using the Xero OpenAPI schema.
The previous SDK is deprecated and we're looking to accelerate our development using the official tooling from Xero.

Is there a known working process you guys are able to share that outlines the process, i've worked in the space for a while and this is the first time that all tools seem to be unable to parse spec files and give something buildable 😬

Expected Behavior

Ability to build a working Golang SDK/client from the in-repo schema

Current Behavior

When building with either the recommended tool - https://github.com/OpenAPITools/openapi-generator or the popular tool oapi-codegen from https://github.com/deepmap/oapi-codegen - many issues arise due to the way that the schema has been structured.

The below is one example trying to validate the schema using a common tool.

go run github.com/getkin/kin-openapi/cmd/validate@latest -- xero_accounting.yaml 
2023/06/23 11:59:11 Loading error: kin-openapi bug found: circular schema reference not handled - #/components/schemas/Payment -> #/components/schemas/CreditNote -> #/components/schemas/Allocation -> #/components/schemas/CreditNote -> #/components/schemas/Payment -> #/components/schemas/Invoice -> #/components/schemas/Prepayment -> #/components/schemas/Allocation -> #/components/schemas/Invoice -> #/components/schemas/Overpayment -> #/components/schemas/Payment -> #/components/schemas/Overpayment -> #/components/schemas/Allocation -> #/components/schemas/Prepayment -> #/components/schemas/Payment
exit status 1

Possible Solution

I wrote a script to contribute that i'll paste below that can go some way to generating the required SDK - including commented out ability to switch generators as needed:

#!/usr/bin/env bash

# Depending on choice, either:
# brew install openapi-generator
# or 
# go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@latest

PACKAGE_NAME=xero
[ -d "$PACKAGE_NAME" ] || mkdir -p "$PACKAGE_NAME"
rm -rf "$PACKAGE_NAME"/

for FILE in *.yaml; do 
    # echo $FILE;
    ORIGINAL_STRIPPED=$(basename $FILE .yaml);
    # Normalise lower case/hyphens
    NEW=$(echo $ORIGINAL_STRIPPED | tr 'xero-' 'xero_' | sed 's/xero_//')
    # Generate the API spec
    echo Generating from "$ORIGINAL_STRIPPED"
    mkdir -p "$PACKAGE_NAME/$NEW/"
    oapi-codegen --package=main -generate=types,client  -package "$PACKAGE_NAME" "$FILE" > "$PACKAGE_NAME/$NEW".gen.go
    # openapi-generator generate -i "$FILE" -g go --additional-properties packageName="$NEW" -o ./"$PACKAGE_NAME/$NEW/" 
done

However these result in duplicate values in packages, unresolvable references or bad type definitions.

Context

We are currently building a financial services integration and would like to be able to use Xero as an approved accounting platform.

Your Environment

  • Version used: main
  • Browser Name and version: Chrome or Safari - any
  • Operating System and version (desktop or mobile): MacOSX
  • Link to your project: n/a at this time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions